onError method

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

The callback will be executed on error.

If you want to continue the error , call handler.next.

If you want to complete the response with some custom data directly, you can resolve a Response object with handler.resolve and other error interceptor(s) will be skipped.

If you want to complete the response with an error message directly, you can reject a DioError object with handler.reject, and other error interceptor(s) will be skipped.

Implementation

@override
void onError(DioError err, ErrorInterceptorHandler handler) {
  final GleapNetworkLog gleapNetworkLog = GleapNetworkLog(
    type: () {
      try {
        return err.requestOptions.method.toUpperCase();
      } catch (_) {
        return null;
      }
    }(),
    url: () {
      try {
        return err.requestOptions.path;
      } catch (_) {
        return null;
      }
    }(),
    date: DateTime.now(),
    request: GleapNetworkRequest(
      headers: () {
        try {
          return _prepareMap(map: err.requestOptions.headers);
        } catch (_) {
          return null;
        }
      }(),
      payload: () {
        try {
          return err.requestOptions.data;
        } catch (_) {
          return '';
        }
      }(),
    ),
    response: GleapNetworkResponse(
      status: () {
        try {
          return err.response?.statusCode;
        } catch (_) {
          return null;
        }
      }(),
      statusText: () {
        try {
          return err.response?.statusMessage;
        } catch (_) {
          return null;
        }
      }(),
      responseText: () {
        try {
          return NetworkResponseTypeHelper.getType(data: err.response?.data);
        } catch (_) {
          return null;
        }
      }(),
    ),
    success: false,
  );

  _updateNetworkLogs(gleapNetworkLog);

  handler.next(err);
}