From dacbf880da2197b7ee6d2580b10499807598191b Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 26 Jul 2022 15:57:16 +1000 Subject: [PATCH] Improve error handling and reporting (#190) * Prevent duplicate reporting of errors to sentry * Prevent error message upload on some server error codes * Filter out some common errors we are not interested in --- lib/api.dart | 31 +++++++++++++++++++------------ lib/inventree/model.dart | 13 +++++++++++++ lib/inventree/sentry.dart | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index c68219f9..5c024895 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -1018,18 +1018,25 @@ class InvenTreeAPI { if (_response.statusCode >= 500) { showStatusCodeError(url, _response.statusCode); - sentryReportMessage( - "Server error", - context: { - "url": request.uri.toString(), - "method": request.method, - "statusCode": _response.statusCode.toString(), - "requestHeaders": request.headers.toString(), - "responseHeaders": _response.headers.toString(), - "responseData": response.data.toString(), - } - ); - + // Some server errors are not ones for us to worry about! + switch (_response.statusCode) { + case 502: // Bad gateway + case 504: // Gateway timeout + break; + default: // Any other error code + sentryReportMessage( + "Server error", + context: { + "url": request.uri.toString(), + "method": request.method, + "statusCode": _response.statusCode.toString(), + "requestHeaders": request.headers.toString(), + "responseHeaders": _response.headers.toString(), + "responseData": response.data.toString(), + } + ); + break; + } } else { if (ignoreResponse) { diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index ffb849a4..04ce6574 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -234,10 +234,23 @@ class InvenTreeModel { return {}; } + /* + * Report error information to sentry, when a model operation fails. + */ Future reportModelError(String title, APIResponse response, {Map context = const {}}) async { String dataString = response.data?.toString() ?? "null"; + // If the response has "errorDetail" set, then the error has already been handled, and there is no need to continue + if (response.errorDetail.isNotEmpty) { + return; + } + + // If the response status code indicates a server error, then this has already been reported + if (response.statusCode >= 500) { + return; + } + if (dataString.length > 500) { dataString = dataString.substring(0, 500); } diff --git a/lib/inventree/sentry.dart b/lib/inventree/sentry.dart index 0e8e6966..72964d61 100644 --- a/lib/inventree/sentry.dart +++ b/lib/inventree/sentry.dart @@ -145,6 +145,9 @@ Future sentryReportMessage(String message, {Map? context}) } +/* + * Report an error message to sentry.io + */ Future sentryReportError(String source, dynamic error, dynamic stackTrace, {Map context = const {}}) async { print("----- Sentry Intercepted error: $error -----"); @@ -166,6 +169,22 @@ Future sentryReportError(String source, dynamic error, dynamic stackTrace, return; } + // Some errors are outside our control, and we do not want to "pollute" the uploaded data + if (source == "FlutterError.onError") { + + String errorString = error.toString(); + + // Missing media file + if (errorString.contains("HttpException") && errorString.contains("404") && errorString.contains("/media/")) { + return; + } + + // Local file system exception + if (errorString.contains("FileSystemException")) { + return; + } + } + final server_info = getServerInfo(); final app_info = await getAppInfo(); final device_info = await getDeviceInfo();