namespace octo_fiesta.Services.Common;
///
/// Represents a typed error with code, message, and metadata
///
public class Error
{
///
/// Unique error code identifier
///
public string Code { get; }
///
/// Human-readable error message
///
public string Message { get; }
///
/// Error type/category
///
public ErrorType Type { get; }
///
/// Additional metadata about the error
///
public Dictionary? Metadata { get; }
private Error(string code, string message, ErrorType type, Dictionary? metadata = null)
{
Code = code;
Message = message;
Type = type;
Metadata = metadata;
}
///
/// Creates a Not Found error (404)
///
public static Error NotFound(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "NOT_FOUND", message, ErrorType.NotFound, metadata);
}
///
/// Creates a Validation error (400)
///
public static Error Validation(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "VALIDATION_ERROR", message, ErrorType.Validation, metadata);
}
///
/// Creates an Unauthorized error (401)
///
public static Error Unauthorized(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "UNAUTHORIZED", message, ErrorType.Unauthorized, metadata);
}
///
/// Creates a Forbidden error (403)
///
public static Error Forbidden(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "FORBIDDEN", message, ErrorType.Forbidden, metadata);
}
///
/// Creates a Conflict error (409)
///
public static Error Conflict(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "CONFLICT", message, ErrorType.Conflict, metadata);
}
///
/// Creates an Internal Server Error (500)
///
public static Error Internal(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "INTERNAL_ERROR", message, ErrorType.Internal, metadata);
}
///
/// Creates an External Service Error (502/503)
///
public static Error ExternalService(string message, string? code = null, Dictionary? metadata = null)
{
return new Error(code ?? "EXTERNAL_SERVICE_ERROR", message, ErrorType.ExternalService, metadata);
}
///
/// Creates a custom error with specified type
///
public static Error Custom(string code, string message, ErrorType type, Dictionary? metadata = null)
{
return new Error(code, message, type, metadata);
}
}
///
/// Categorizes error types for appropriate HTTP status code mapping
///
public enum ErrorType
{
///
/// Validation error (400 Bad Request)
///
Validation,
///
/// Resource not found (404 Not Found)
///
NotFound,
///
/// Authentication required (401 Unauthorized)
///
Unauthorized,
///
/// Insufficient permissions (403 Forbidden)
///
Forbidden,
///
/// Resource conflict (409 Conflict)
///
Conflict,
///
/// Internal server error (500 Internal Server Error)
///
Internal,
///
/// External service error (502 Bad Gateway / 503 Service Unavailable)
///
ExternalService
}