onError method

  1. @override
void onError(
  1. DioException error,
  2. ErrorInterceptorHandler handler
)

Handles error and adds data to alice http call

Implementation

@override
void onError(
  DioException error,
  ErrorInterceptorHandler handler,
) {
  var httpError = AliceHttpError();
  httpError.error = error.toString();
  if (error is Error) {
    var basicError = error as Error;
    httpError.stackTrace = basicError.stackTrace;
  }

  aliceCore.addError(httpError, error.requestOptions.hashCode);
  var httpResponse = AliceHttpResponse();
  httpResponse.time = DateTime.now();
  if (error.response == null) {
    httpResponse.status = -1;
    aliceCore.addResponse(httpResponse, error.requestOptions.hashCode);
  } else {
    httpResponse.status = error.response!.statusCode!;

    if (error.response!.data == null) {
      httpResponse.body = "";
      httpResponse.size = 0;
    } else {
      httpResponse.body = error.response!.data;
      httpResponse.size = utf8.encode(error.response!.data.toString()).length;
    }
    Map<String, String> headers = Map();
    if (error.response?.headers != null) {
      error.response!.headers.forEach((header, values) {
        headers[header] = values.toString();
      });
    }
    httpResponse.headers = headers;
    aliceCore.addResponse(
        httpResponse, error.response!.requestOptions.hashCode);
  }
  handler.next(error);
}