Skip to content

Instantly share code, notes, and snippets.

@iducool
Created January 26, 2021 09:16
Show Gist options
  • Save iducool/f80a466f423c34ec65c6230f54571500 to your computer and use it in GitHub Desktop.
Save iducool/f80a466f423c34ec65c6230f54571500 to your computer and use it in GitHub Desktop.
Sentry web services tracking feature with Dio API client
import 'package:dio/dio.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
class SentryInterceptor extends InterceptorsWrapper {
@override
Future onResponse(Response response) {
Sentry.addBreadcrumb(
Breadcrumb(
type: 'http',
category: 'http',
data: {
'url': response.request.uri.toString(),
'method': response.request.method,
'status_code': response.statusCode,
},
),
);
return super.onResponse(response);
}
@override
Future onError(DioError err) {
Sentry.addBreadcrumb(
Breadcrumb(
type: 'http',
category: 'http',
data: {
'url': err.request.uri.toString(),
'method': err.request.method,
'status_code': err.response?.statusCode ?? "NA",
},
message: DioErrorHandler.dioErrorToString(err),
),
);
return super.onError(err);
}
}
class DioErrorHandler {
static String dioErrorToString(DioError dioError) {
String errorText;
switch (dioError.type) {
case DioErrorType.CONNECT_TIMEOUT:
errorText =
"Connection Timeout. Check your Internet connection or contact Server adiministrator";
break;
case DioErrorType.RECEIVE_TIMEOUT:
case DioErrorType.SEND_TIMEOUT:
errorText =
"Connection lost, please check your internet connection and try again.";
break;
case DioErrorType.RESPONSE:
errorText = _errorBaseOnHttpStatusCode(dioError);
break;
case DioErrorType.DEFAULT:
errorText =
"Connection lost, please check your internet connection and try again.";
break;
case DioErrorType.CANCEL:
errorText =
"Connection lost, please check your internet connection and try again.";
break;
}
return errorText;
}
static String _errorBaseOnHttpStatusCode(DioError dioError) {
String errorText;
if (dioError.response != null) {
if (dioError.response.statusCode == 401) {
errorText =
"Something went wrong, please close the app and login again.";
} else if (dioError.response.statusCode == 404) {
errorText =
"Connection lost, please check your internet connection and try again.";
} else if (dioError.response.statusCode == 500) {
errorText =
"We couldn't connect to the product server. Please contact Server adiminstrator";
} else {
errorText =
"Something went wrong, please close the app and login again. If the issue persists contact Server adiminstrator";
}
} else {
errorText =
"Something went wrong, please close the app and login again. If the issue persists contact Server adiminstrator";
}
return errorText;
}
}
@iducool
Copy link
Author

iducool commented Jan 26, 2021

Here is a helper link: [https://pub.dev/packages/dio#interceptors](Dio Interceptor)

Way of adding interceptor in client:
_dio.interceptors.add(SentryInterceptor());

@akul1994
Copy link

`
import 'package:dio/dio.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

class SentryInterceptor extends InterceptorsWrapper {
@OverRide
void onResponse(Response response, ResponseInterceptorHandler handler) {
Sentry.addBreadcrumb(
Breadcrumb(
type: 'http',
category: 'http',
data: {
'url': response.requestOptions.uri.toString(),
'method': response.requestOptions.method,
'status_code': response.statusCode,
},
message: 'API SUCCESS',
),
);
super.onResponse(response, handler);
}

@OverRide
void onError(DioError err, ErrorInterceptorHandler handler) {
Sentry.addBreadcrumb(
Breadcrumb(
type: 'http',
category: 'http',
data: {
'url': err.requestOptions.uri.toString(),
'method': err.requestOptions.method,
'status_code': err.response?.statusCode ?? "NA",
},
message: 'API FAILURE',
),
);
super.onError(err, handler);
}
}
`

@akul1994
Copy link

Updated for null safety

@sooxt98
Copy link

sooxt98 commented Dec 12, 2021

Fix syntax and added support for latest dio & sentry

import 'package:dio/dio.dart';
import 'package:sentry/sentry.dart';

class SentryInterceptor extends InterceptorsWrapper {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    Sentry.addBreadcrumb(
      Breadcrumb(
        type: 'http',
        category: 'http',
        data: {
          'url': response.requestOptions.uri.toString(),
          'method': response.requestOptions.method,
          'status_code': response.statusCode,
        },
      ),
    );
    super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    Sentry.addBreadcrumb(
      Breadcrumb(
        type: 'http',
        category: 'http',
        data: {
          'url': err.requestOptions.uri.toString(),
          'method': err.requestOptions.method,
          'status_code': err.response?.statusCode ?? "NA",
        },
        message: DioErrorHandler.dioErrorToString(err),
      ),
    );
    super.onError(err, handler);
  }
}

class DioErrorHandler {
  static String dioErrorToString(DioError dioError) {
    String errorText;
    switch (dioError.type) {
      case DioErrorType.connectTimeout:
        errorText =
            "Connection Timeout. Check your Internet connection or contact Server adiministrator";
        break;
      case DioErrorType.receiveTimeout:
      case DioErrorType.sendTimeout:
        errorText =
            "Connection lost, please check your internet connection and try again.";
        break;
      case DioErrorType.response:
        errorText = _errorBaseOnHttpStatusCode(dioError);
        break;
      case DioErrorType.other:
        errorText =
            "Connection lost, please check your internet connection and try again.";
        break;
      case DioErrorType.cancel:
        errorText =
            "Connection lost, please check your internet connection and try again.";
        break;
    }
    return errorText;
  }

  static String _errorBaseOnHttpStatusCode(DioError dioError) {
    String errorText;
    if (dioError.response != null) {
      if (dioError.response?.statusCode == 401) {
        errorText =
            "Something went wrong, please close the app and login again.";
      } else if (dioError.response?.statusCode == 404) {
        errorText =
            "Connection lost, please check your internet connection and try again.";
      } else if (dioError.response?.statusCode == 500) {
        errorText =
            "We couldn't connect to the product server. Please contact Server adiminstrator";
      } else {
        errorText =
            "Something went wrong, please close the app and login again. If the issue persists contact Server adiminstrator";
      }
    } else {
      errorText =
          "Something went wrong, please close the app and login again. If the issue persists contact Server adiminstrator";
    }

    return errorText;
  }
}

@Toasbi
Copy link

Toasbi commented Mar 25, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment