ApiException.fromResponse constructor

ApiException.fromResponse(
  1. Response response
)

Implementation

factory ApiException.fromResponse(Response response) {
  String? errorMessage;
  if (response.body.isNotEmpty) {
    try {
      var decodedBody = jsonDecode(response.body);
      if (decodedBody is Map<String, dynamic>) {
        // TODO(xha): find out the format
        errorMessage = decodedBody['message'] as String? ??
            decodedBody['errorMessage'] as String?;
      } else {
        decodedBody = '$decodedBody';
      }
    } catch (e) {
      // Fail to parse as Json
    }
    errorMessage ??= response.body;
  }
  return ApiException(
      response.request?.url, response.statusCode, response.reasonPhrase,
      errorMessage: errorMessage);
}