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) {
  final httpError = AliceHttpError()..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()..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 = ''
        ..size = 0;
    } else {
      httpResponse
        ..body = error.response!.data
        ..size = utf8.encode(error.response!.data.toString()).length;
    }
    final headers = <String, String>{};
    error.response!.headers.forEach((header, values) {
      headers[header] = values.toString();
    });
    httpResponse.headers = headers;
    aliceCore.addResponse(
      httpResponse,
      error.response!.requestOptions.hashCode,
    );
    aliceCore.addLog(AliceLog(
        message: error.toString(),
        level: DiagnosticLevel.error,
        error: error,
        stackTrace: error.stackTrace));
  }
  handler.next(error);
}