exception method
This extension allows for a streamlined way to instantiate
HttpException objects directly from integer status codes.
It leverages the HttpStatus.fromCode
method to create the
corresponding HttpException for the given status code.
Additional details such as a custom message, data payload,
and URI can be optionally provided.
Example Usage:
final exception = 404.exception(detail: 'Not Found', uri: Uri.parse('https://example.com'));
print(exception); // Outputs the generated HttpException for a 404 status code.
Parameters:
-
detail
: A string providing additional details about the exception. This detail message can be used to give more context about the error that occurred. It defaults to an empty string if no detail is provided. -
data
: An optional map of key-value pairs that can contain any additional data you want to associate with the exception. This can be useful for passing structured error information that can be programmatically processed. Defaults tonull
if no data is provided. -
uri
: An optional Uri object indicating the URI associated with the exception. This can be particularly useful for logging or tracing requests that resulted in an error. Defaults tonull
if no URI is provided.
Returns: A HttpException object that corresponds to the HTTP status code it's called on, populated with any provided details, data, and URI.
Implementation
HttpException exception({
String detail = '',
Map<String, dynamic>? data,
Uri? uri,
}) {
final HttpException httpStatus = HttpStatus.fromCode(this).exception(
detail: detail,
data: data,
uri: uri,
);
return httpStatus;
}