utils/client/helpers/settle library
A utility class for handling and settling HTTP responses in a standardized way.
The Settle class provides static methods to parse, validate, and extract information from HTTP responses, including error handling and optional authentication expiration triggers. It is designed to be configured globally and used throughout your application to ensure consistent response handling.
Example usage:
Settle.configure(
onForceOut: () {
// Handle forced logout or session expiration
},
errorMessageExtractor: (response) {
// Custom error message extraction logic
return "Custom error: $response";
},
);
final result = Settle.settleResponse<MyModel>(
responseString,
fromJsonT: (json) => MyModel.fromJson(json),
wrapper: (json) => MyModel.fromJson(json),
);
Typedefs:
- FromJson is a function that converts a JSON object to a Dart object.
- OnForceOutCallback is a callback triggered when authentication expires.
- ErrorMessageExtractor extracts an error message from a response string.
- ResponseWrapper wraps the parsed JSON into the desired response type.
Methods:
configuresets up global callbacks and error extractors.settleResponseparses and validates the response, returning the wrapped result or throwing a FinnUtilsSettlerException on error._triggerForceOutIfNeededchecks for authentication expiration and triggers the callback if needed._defaultErrorMessageExtractorprovides a default way to extract error messages from responses._safeJsonDecodesafely decodes a JSON string into a map.
Throws:
- FinnUtilsSettlerException if the response is invalid, contains an error, or if session expiration is detected.
See also:
- FinnUtilsSettlerException for custom exception handling.
Classes
Typedefs
- ErrorMessageExtractor = String Function(String? responseString)
-
FromJson<
T> = T Function(dynamic json) - OnForceOutCallback = void Function()
-
ResponseWrapper<
T> = T Function(Map< String, dynamic> json)