ServiceException.fromHttpResponse constructor

ServiceException.fromHttpResponse(
  1. BaseResponse response,
  2. String? responseBody
)

Create a ServiceException (or appropriate subclass) from an HTTP response.

Implementation

factory ServiceException.fromHttpResponse(
  http.BaseResponse response,
  String? responseBody,
) {
  if (responseBody == null || responseBody.isEmpty) {
    return ServiceException._fromDecodedResponse(
      'unknown error',
      response: response,
      responseBody: responseBody,
    );
  }

  final dynamic json;
  try {
    json = jsonDecode(responseBody);
  } on FormatException {
    return ServiceException._fromDecodedResponse(
      responseBody,
      response: response,
      responseBody: responseBody,
    );
  }

  final Status status;
  if (json is Map<String, dynamic> && json['error'] is Map<String, dynamic>) {
    status = Status.fromJson(json['error'] as Map<String, dynamic>);
  } else {
    return ServiceException._fromDecodedResponse(
      responseBody,
      response: response,
      responseBody: responseBody,
    );
  }

  return ServiceException._fromDecodedResponse(
    status.message,
    response: response,
    responseBody: responseBody,
    status: status,
  );
}