Class CurlException
- Namespace
- CurlDotNet.Exceptions
- Assembly
- CurlDotNet.dll
Base exception for all curl operations. This is the base class for all curl-specific exceptions.
[Serializable]
public class CurlException : Exception, ISerializable
- Inheritance
-
CurlException
- Implements
- Derived
- Inherited Members
Examples
try
{
var result = await curl.ExecuteAsync("curl https://api.example.com");
}
catch (CurlConnectionException ex)
{
// Handle connection-specific issues
Console.WriteLine($"Failed to connect to {ex.Host}:{ex.Port}");
}
catch (CurlException ex)
{
// Handle any other curl error
Console.WriteLine($"Curl failed: {ex.Message}");
Console.WriteLine($"Command: {ex.Command}");
Console.WriteLine($"Error code: {ex.CurlErrorCode}");
}
Remarks
This exception provides common properties for all curl errors including the command that was executed and the curl error code.
Curl error codes match the original curl error codes from the C implementation.
AI-Usage: Catch this exception type to handle any curl-related error generically.
AI-Pattern: Use specific derived exceptions for targeted error handling.
Constructors
CurlException(SerializationInfo, StreamingContext)
Initializes a new instance of the CurlException class with serialized data.
protected CurlException(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoThe serialization information.
contextStreamingContextThe streaming context.
CurlException(string, int, string)
Initializes a new instance of the CurlException class with a curl error code.
public CurlException(string message, int curlErrorCode, string command = null)
Parameters
messagestringThe error message that explains the reason for the exception.
curlErrorCodeintThe curl error code from the original curl implementation.
commandstringThe curl command that was executing when the error occurred.
CurlException(string, string, Exception)
Initializes a new instance of the CurlException class with a specified error message.
public CurlException(string message, string command = null, Exception innerException = null)
Parameters
messagestringThe error message that explains the reason for the exception.
commandstringThe curl command that was executing when the error occurred.
innerExceptionExceptionThe exception that is the cause of the current exception.
Properties
Command
Gets the curl command that was being executed when the exception occurred.
public string Command { get; }
Property Value
- string
The full curl command string, or null if not applicable.
Remarks
This property contains the exact command that was passed to the Execute method.
AI-Usage: Use this for logging and debugging to understand what command failed.
Context
Gets additional context information added via fluent methods.
public Dictionary<string, object> Context { get; }
Property Value
CurlErrorCode
Gets the curl error code matching the original curl implementation.
public int? CurlErrorCode { get; }
Property Value
- int?
The curl error code (e.g., 6 for DNS resolution failure, 28 for timeout), or null if not a curl-specific error.
Remarks
Error codes match the CURLE_* constants from curl.h
Common codes: 6=DNS failure, 7=connection failed, 28=timeout, 35=SSL error
AI-Usage: Use this to determine the specific type of curl error programmatically.
DiagnosticInfo
Gets or sets custom diagnostic information.
public string DiagnosticInfo { get; set; }
Property Value
Suggestions
Gets suggestions for resolving this error.
public List<string> Suggestions { get; }
Property Value
Methods
GetCurlErrorName()
Get the CURLE_* constant name for the error code.
public string GetCurlErrorName()
Returns
- string
Curl error constant name
GetDiagnosticInfo()
Get diagnostic information for debugging.
public virtual Dictionary<string, object> GetDiagnosticInfo()
Returns
- Dictionary<string, object>
Diagnostic details
GetObjectData(SerializationInfo, StreamingContext)
Sets the SerializationInfo with information about the exception.
public override void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoThe serialization information.
contextStreamingContextThe streaming context.
IsRetryable()
Check if this error is potentially retryable.
public virtual bool IsRetryable()
Returns
- bool
True if the error might succeed on retry
Log(Action<string, Dictionary<string, object>>)
Log this exception with structured data.
public void Log(Action<string, Dictionary<string, object>> logger)
Parameters
loggerAction<string, Dictionary<string, object>>Action to perform logging
ToDetailedString()
Get a detailed string representation of the exception.
public virtual string ToDetailedString()
Returns
- string
Detailed error information
ToJson()
Get the exception as a structured JSON object for logging.
public virtual string ToJson()
Returns
- string
JSON representation of the exception
ToUserFriendlyMessage()
Create a user-friendly error message.
public virtual string ToUserFriendlyMessage()
Returns
- string
Simplified error message for end users
WithContext(Dictionary<string, object>)
Add multiple context values (fluent).
public CurlException WithContext(Dictionary<string, object> context)
Parameters
contextDictionary<string, object>Dictionary of context values
Returns
- CurlException
This exception for chaining
WithContext(string, object)
Add contextual information to the exception (fluent).
public CurlException WithContext(string key, object value)
Parameters
Returns
- CurlException
This exception for chaining
WithDiagnostics(string)
Add diagnostic information (fluent).
public CurlException WithDiagnostics(string diagnosticInfo)
Parameters
diagnosticInfostringDiagnostic details
Returns
- CurlException
This exception for chaining
WithSuggestion(string)
Add a suggestion for resolving this error (fluent).
public CurlException WithSuggestion(string suggestion)
Parameters
suggestionstringHelpful suggestion text
Returns
- CurlException
This exception for chaining