mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-31 13:25:40 +00:00 
			
		
		
		
	Upload custom message when server returns error code >= 500
This commit is contained in:
		
							
								
								
									
										43
									
								
								lib/api.dart
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								lib/api.dart
									
									
									
									
									
								
							| @@ -525,6 +525,18 @@ class InvenTreeAPI { | |||||||
|       print("Data:"); |       print("Data:"); | ||||||
|       print(responseData); |       print(responseData); | ||||||
|  |  | ||||||
|  |       // Server error | ||||||
|  |       if (response.statusCode >= 500) { | ||||||
|  |         sentryReportMessage( | ||||||
|  |             "Server error on PATCH request", | ||||||
|  |             context: { | ||||||
|  |               "url": _url, | ||||||
|  |               "statusCode": "${response.statusCode}", | ||||||
|  |               "data": responseData.toString(), | ||||||
|  |             } | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |  | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -660,6 +672,18 @@ class InvenTreeAPI { | |||||||
|       print("Data:"); |       print("Data:"); | ||||||
|       print(responseData); |       print(responseData); | ||||||
|  |  | ||||||
|  |       // Server error | ||||||
|  |       if (response.statusCode >= 500) { | ||||||
|  |         sentryReportMessage( | ||||||
|  |           "Server error on POST request", | ||||||
|  |           context: { | ||||||
|  |             "url": _url, | ||||||
|  |             "statusCode": "${response.statusCode}", | ||||||
|  |             "data": responseData.toString(), | ||||||
|  |           } | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |  | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -863,15 +887,28 @@ class InvenTreeAPI { | |||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     var responseData = await responseToJson(response); | ||||||
|  |  | ||||||
|     // Check the status code of the response |     // Check the status code of the response | ||||||
|     if (response.statusCode != expectedStatusCode) { |     if (response.statusCode != expectedStatusCode) { | ||||||
|       showStatusCodeError(response.statusCode); |       showStatusCodeError(response.statusCode); | ||||||
|  |  | ||||||
|  |       // Server error | ||||||
|  |       if (response.statusCode >= 500) { | ||||||
|  |         sentryReportMessage( | ||||||
|  |             "Server error on GET request", | ||||||
|  |             context: { | ||||||
|  |               "url": url, | ||||||
|  |               "statusCode": "${response.statusCode}", | ||||||
|  |               "data": responseData.toString(), | ||||||
|  |             } | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |  | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     var data = await responseToJson(response); |     return responseData; | ||||||
|  |  | ||||||
|     return data; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Map<String, String> defaultHeaders() { |   Map<String, String> defaultHeaders() { | ||||||
|   | |||||||
| @@ -84,6 +84,40 @@ bool isInDebugMode() { | |||||||
|   return inDebugMode; |   return inDebugMode; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | Future<bool> sentryReportMessage(String message, {Map<String, String>? context}) async { | ||||||
|  |  | ||||||
|  |   final server_info = getServerInfo(); | ||||||
|  |   final app_info = await getAppInfo(); | ||||||
|  |   final device_info = await getDeviceInfo(); | ||||||
|  |  | ||||||
|  |   print("Sending user message to Sentry: ${message}"); | ||||||
|  |  | ||||||
|  |   if (isInDebugMode()) { | ||||||
|  |  | ||||||
|  |     print('----- In dev mode. Not sending message to Sentry.io -----'); | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   Sentry.configureScope((scope) { | ||||||
|  |     scope.setExtra("server", server_info); | ||||||
|  |     scope.setExtra("app", app_info); | ||||||
|  |     scope.setExtra("device", device_info); | ||||||
|  |  | ||||||
|  |     if (context != null) { | ||||||
|  |       scope.setExtra("context", context); | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  |  | ||||||
|  |   final sentryId = await Sentry.captureMessage(message).catchError((error) { | ||||||
|  |     print("Error uploading sentry messages..."); | ||||||
|  |     print(error); | ||||||
|  |     return null; | ||||||
|  |   }); | ||||||
|  |  | ||||||
|  |   return sentryId != null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Future<void> sentryReportError(dynamic error, dynamic stackTrace) async { | Future<void> sentryReportError(dynamic error, dynamic stackTrace) async { | ||||||
|  |  | ||||||
|   print('----- Sentry Intercepted error: $error -----'); |   print('----- Sentry Intercepted error: $error -----'); | ||||||
| @@ -115,27 +149,3 @@ Future<void> sentryReportError(dynamic error, dynamic stackTrace) async { | |||||||
|     print("Uploaded information to Sentry.io : ${response.toString()}"); |     print("Uploaded information to Sentry.io : ${response.toString()}"); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Future<bool> sentryReportMessage(String message) async { |  | ||||||
|  |  | ||||||
|   final server_info = getServerInfo(); |  | ||||||
|   final app_info = await getAppInfo(); |  | ||||||
|   final device_info = await getDeviceInfo(); |  | ||||||
|  |  | ||||||
|   print("Sending user message to Sentry"); |  | ||||||
|  |  | ||||||
|   Sentry.configureScope((scope) { |  | ||||||
|     scope.setExtra("server", server_info); |  | ||||||
|     scope.setExtra("app", app_info); |  | ||||||
|     scope.setExtra("device", device_info); |  | ||||||
|   }); |  | ||||||
|  |  | ||||||
|   final sentryId = await Sentry.captureMessage(message).catchError((error) { |  | ||||||
|     print("Error uploading sentry messages..."); |  | ||||||
|     print(error); |  | ||||||
|     return null; |  | ||||||
|   }); |  | ||||||
|  |  | ||||||
|   return sentryId != null; |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user