processHttpResponse function
Processes an HTTP response and returns a ProcessedResponse object.
This function takes an HTTP response as input and evaluates its status code.
If the status code indicates success (200, 201, or 202), a ProcessedResponse
object is returned with the 'ok' field set to true
. For other status codes,
the response body is decoded as JSON, and an error ProcessedResponse
object
is returned. The extractErrorCode
function is used to extract an error code,
and the getUserFriendlyMsg
and getErrorMessage
functions are used to generate
error messages based on the error code and status code.
@param response The HTTP response to be processed. @return A ProcessedResponse object containing the processed information.
Implementation
ProcessedResponse processHttpResponse(Response response) {
if (<int>[200, 201, 202].contains(response.statusCode)) {
return ProcessedResponse(ok: true, response: response, code: 200);
}
final Map<String, dynamic> body =
json.decode(response.body) as Map<String, dynamic>;
final int code = extractErrorCode(body);
if (response.statusCode == 400 || response.statusCode == 500) {
return ProcessedResponse(
ok: false,
response: response,
message: getUserFriendlyMsg(code),
code: code,
);
}
if (response.statusCode == 408) {
return ProcessedResponse(
ok: false,
response: response,
message: getUserFriendlyMsg(0),
code: 0,
);
}
return ProcessedResponse(
ok: false,
response: response,
message: getErrorMessage(),
code: code,
);
}