HttpMessageNotWritableException class final
Thrown when the HTTP response body could not be written.
Overview
The HttpMessageNotWritableException signals that an HTTP response body could not be successfully serialized or written to the output stream. This exception typically occurs during response generation when attempting to convert data to the desired output format.
Example
try {
final jsonString = jsonEncode(complexObject);
response.write(jsonString);
} on JsonUnsupportedObjectError catch (e, stackTrace) {
throw HttpMessageNotWritableException(
'Failed to serialize response object',
details: {'objectType': complexObject.runtimeType},
originalException: e,
originalStackTrace: stackTrace,
);
}
Common Scenarios
- Serialization Errors - Circular references in JSON serialization
- Encoding Issues - Invalid character sequences during text encoding
- I/O Errors - Closed or interrupted output streams
- Buffer Overflows - Response exceeds buffer capacity limits
Integration Example
try {
await response.writeObject(userData);
} on HttpMessageNotWritableException catch (e) {
logger.error('Response serialization failed', e);
response.reset(); // Clear any partially written content
response.statusCode = e.statusCode;
response.write('Unable to generate response');
}
Design Notes
- Extends HttpException for consistent HTTP error handling
- Default status code 500 Internal Server Error indicates server-side issue
- Preserves original exception and stack trace for debugging
- Includes URI context when available for request identification
Related Exceptions
- HttpMessageNotReadableException - Request body reading failures
HttpMediaTypeException- Response content type negotiation failures
Summary
The HttpMessageNotWritableException provides structured error reporting for response body generation failures, ensuring reliable error recovery and comprehensive server-side monitoring.
- Inheritance
-
- Object
- Error
- HttpException
- HttpMessageNotWritableException
- Implemented types
Constructors
-
HttpMessageNotWritableException(String message, {Map<
String, dynamic> ? details, Exception? originalException, StackTrace? originalStackTrace, int statusCode = 500, Uri? uri}) - Thrown when the HTTP response body could not be written.
Properties
- cause → Object?
-
The underlying cause of this exception, if any.
finalinherited
-
details
→ Map<
String, dynamic> ? -
Additional contextual information about the error.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- message → String
-
The message describing the error.
finalinherited
- originalException → Exception?
-
The underlying exception that caused this FluxException.
finalinherited
- originalStackTrace → StackTrace?
-
The stack trace from the original exception.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stackTrace → StackTrace
-
The associated stack trace.
finalinherited
- statusCode → int
-
The HTTP status code returned by the server, when applicable.
finalinherited
- uri → Uri?
-
The URI of the HTTP request that triggered this exception.
finalinherited
Methods
-
getCause(
) → Object -
The cause of this exception, if any.
inherited
-
getMessage(
) → String -
The message associated with this exception.
inherited
-
getStackTrace(
) → StackTrace -
The stack trace associated with this exception.
inherited
-
getStatus(
) → HttpStatus -
Resolves the corresponding HttpStatus for this object, if available.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, Object> -
inherited
-
toMap(
) → Map< String, dynamic> -
Converts this exception into a serializable map representation.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited