onError method

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

Handles error and adds data to alice http call

Implementation

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

  aliceCore.addError(httpError, error.requestOptions.hashCode);
  final 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;
    }
    final Map<String, String> headers = {};
    error.response!.headers.forEach((header, values) {
      headers[header] = values.toString();
    });
    httpResponse.headers = headers;
    aliceCore.addResponse(
      httpResponse,
      error.response!.requestOptions.hashCode,
    );
  }
  handler.next(error);
}