2018-05-29 19:45:25 -04:00
using Vanara.PInvoke ;
using static Vanara . PInvoke . BITS ;
2023-03-31 11:47:53 -04:00
namespace Vanara.IO ;
/// <summary>Exceptions specific to BITS</summary>
public class BackgroundCopyException : Exception
2018-05-29 19:45:25 -04:00
{
2023-03-31 11:47:53 -04:00
private readonly HRESULT code ;
private readonly BG_ERROR_CONTEXT ctx ;
2023-08-23 16:28:45 -04:00
private readonly string? ctxDesc , errDesc , protocol ;
private readonly IBackgroundCopyFile ? iVal ;
2023-03-31 11:47:53 -04:00
internal BackgroundCopyException ( IBackgroundCopyError err )
2018-05-29 19:45:25 -04:00
{
2023-03-31 11:47:53 -04:00
const uint lang = 0x1 ; // LANG_NEUTRAL, SUBLANG_DEFAULT
err . GetError ( out ctx , out code ) ;
try { ctxDesc = err . GetErrorContextDescription ( lang ) ; }
catch { ctxDesc = SafeCoTaskMemString . Null ; }
try { errDesc = err . GetErrorDescription ( lang ) ; }
catch { errDesc = SafeCoTaskMemString . Null ; }
try { protocol = err . GetProtocol ( ) ; }
catch { protocol = SafeCoTaskMemString . Null ; }
iVal = err . GetFile ( ) ;
2018-05-29 19:45:25 -04:00
}
2023-03-31 11:47:53 -04:00
internal BackgroundCopyException ( COMException cex )
{
code = cex . ErrorCode ;
errDesc = BackgroundCopyManager . GetErrorMessage ( code ) ;
if ( errDesc is null )
code . ThrowIfFailed ( ) ;
}
/// <summary>Context in which the error occurred.</summary>
public BackgroundCopyErrorContext Context = > ( BackgroundCopyErrorContext ) ctx ;
/// <summary>Description of the context in which the error occurred.</summary>
2023-08-23 16:28:45 -04:00
public string? ContextDescription = > ctxDesc ;
2023-03-31 11:47:53 -04:00
/// <summary>Gets the error code.</summary>
/// <value>The error code.</value>
public HRESULT ErrorCode = > code ;
/// <summary>If error was related to a file, returns information about the file and its progress. Otherwise, returns NULL.</summary>
2023-08-23 16:28:45 -04:00
public BackgroundCopyFileInfo ? File
2023-03-31 11:47:53 -04:00
{
get { if ( iVal is null ) return null ; return new BackgroundCopyFileInfo ( iVal ) ; }
}
/// <summary>The error text associated with the error.</summary>
2023-08-23 16:28:45 -04:00
public override string Message = > errDesc ? ? "" ;
2023-03-31 11:47:53 -04:00
/// <summary>
/// Contains the protocol used to transfer the file. The string contains "http" for the HTTP protocol and "file" for the SMB protocol and to NULL if the
/// error is not related to the transfer protocol.
/// </summary>
2023-08-23 16:28:45 -04:00
public string? Protocol = > protocol ;
2018-05-29 19:45:25 -04:00
}