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;
}
}
@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