Class HttpErrorMapper
- Namespace
- CivitaiSharp.Core.Response
- Assembly
- CivitaiSharp.Core.dll
Provides utilities for mapping HTTP responses to Error instances. Centralizes HTTP error handling logic including RFC 7807 problem details parsing for consistent behavior across CivitaiSharp libraries.
public static class HttpErrorMapper
- Inheritance
-
HttpErrorMapper
- Inherited Members
Remarks
This class is public to allow custom HTTP client implementations to use the same error mapping logic.
When parsing RFC 7807 error responses, only the standard fields useful for client-side error handling are extracted (type, title, status, detail, instance, errors, traceId). Custom RFC 7807 extension fields are intentionally not exposed as they are typically server-implementation-specific and not actionable by client code.
Methods
BuildCloudflareErrorMessage(string, int, string)
Builds a detailed error message for Cloudflare errors.
public static string BuildCloudflareErrorMessage(string errorType, int statusCode, string contentType)
Parameters
errorTypestringThe detected Cloudflare error type.
statusCodeintThe HTTP status code.
contentTypestringThe content type of the response.
Returns
- string
A formatted error message.
DetectCloudflareErrorType(string)
Detects the type of Cloudflare error page from the response content.
public static string? DetectCloudflareErrorType(string content)
Parameters
contentstringThe HTML content of the response.
Returns
- string
The detected Cloudflare error type, or null if not a Cloudflare error.
FromHttpRequestException(HttpRequestException)
Creates an Error from an HttpRequestException.
public static Error FromHttpRequestException(HttpRequestException exception)
Parameters
exceptionHttpRequestExceptionThe HTTP request exception.
Returns
- Error
An error representing the exception.
FromHttpResponse(HttpStatusCode, string?, string?, RetryConditionHeaderValue?)
Creates an Error from an HTTP response.
public static Error FromHttpResponse(HttpStatusCode statusCode, string? content, string? reasonPhrase, RetryConditionHeaderValue? retryAfter)
Parameters
statusCodeHttpStatusCodeThe HTTP status code.
contentstringThe response body content, if available.
reasonPhrasestringThe HTTP reason phrase, if available.
retryAfterRetryConditionHeaderValueThe Retry-After header value, if available.
Returns
- Error
An error representing the HTTP response.
FromTimeout(Exception?)
Creates an Error for a timeout.
public static Error FromTimeout(Exception? exception = null)
Parameters
exceptionExceptionThe exception that caused the timeout.
Returns
- Error
An error representing the timeout.
GetRetryAfter(RetryConditionHeaderValue?)
Extracts the Retry-After duration from response headers.
public static TimeSpan? GetRetryAfter(RetryConditionHeaderValue? retryAfter)
Parameters
retryAfterRetryConditionHeaderValueThe Retry-After header value.
Returns
- TimeSpan?
The retry delay, or null if not specified.
MapStatusCode(HttpStatusCode)
Maps an HTTP status code to the corresponding ErrorCode.
public static ErrorCode MapStatusCode(HttpStatusCode statusCode)
Parameters
statusCodeHttpStatusCodeThe HTTP status code to map.
Returns
- ErrorCode
The corresponding error code.