Skip to main content
Version: 0.5.x

Exception Handler

Sharkable provides a built-in global exception handler middleware that converts unhandled exceptions into consistent UnifiedResult<T> JSON responses.

Global Exception Handler

Enabled by default when calling app.UseShark(). Catches all unhandled exceptions from endpoints and middleware, maps them to HTTP status codes, and returns a UnifiedResult<object?> JSON body.

Default status code mappings

Exception typeHTTP status
KeyNotFoundException404 Not Found
UnauthorizedAccessException401 Unauthorized
ArgumentException400 Bad Request
Any other exception500 Internal Server Error

Response format

{
"StatusCode": 404,
"Data": null,
"ErrorMessage": "user not found",
"Extra": null,
"TimeStamp": 1780881487673,
"Result": null
}

In development mode (ASPNETCORE_ENVIRONMENT=Development), the error message includes the full stack trace.

Custom error mapping

app.UseShark(opt => { });

// Or standalone:
app.UseSharkExceptionHandler(opt =>
{
opt.Map<MyCustomException>(HttpStatusCode.Forbidden);
});

Disable

app.UseShark(opt =>
{
opt.EnableExceptionHandler = false;
});