buildParseResponseWithException function

ParseResponse buildParseResponseWithException(
  1. Exception exception
)

Handles exception instead of throwing an exception

Implementation

ParseResponse buildParseResponseWithException(Exception exception) {
  if (exception is DioException) {
    Map<String, dynamic> errorResponse = {};
    try {
      errorResponse = json.decode(exception.response?.data?.toString() ?? '{}');
    } on FormatException catch (_) {}

    final errorMessage =
        errorResponse['error']?.toString() ?? exception.response?.statusMessage;

    final String? codeString = errorResponse['code']?.toString();
    final errorCode =
        int.tryParse(codeString ?? '') ?? exception.response?.statusCode;

    return ParseResponse(
      error: ParseError(
        message: errorMessage ?? exception.toString(),
        exception: exception,
        code: errorCode ?? ParseError.otherCause,
      ),
    );
  }

  if (exception is ClientException) {
    return ParseResponse(
      error: ParseError(message: exception.message, exception: exception),
    );
  }

  return ParseResponse(
    error: ParseError(message: exception.toString(), exception: exception),
  );
}