mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-22 21:56:06 +00:00
Improve barcode error handling (#910)
* Improve barcode error handling Exit early if API call fails * fix type of default values * tweak wedge scanner
This commit is contained in:
1 parent
08f78763f4
commit
93d47175c8
4 files changed
+28
-8
No files matched your search
+3
-1
@@ -42,7 +42,7 @@ class APIResponse {
|
||||
this.method = "",
|
||||
this.statusCode = -1,
|
||||
this.error = "",
|
||||
this.data = const {},
|
||||
this.data = const <String, dynamic>{},
|
||||
});
|
||||
|
||||
int statusCode = -1;
|
||||
@@ -1816,6 +1816,8 @@ class InvenTreeAPI {
|
||||
debug("CertificateException at ${request.uri.toString()}:");
|
||||
debug(error.toString());
|
||||
showServerError(url, L10().serverCertificateError, error.toString());
|
||||
response.error = "CertificateException";
|
||||
response.errorDetail = error.toString();
|
||||
} on TimeoutException {
|
||||
showTimeoutError(url);
|
||||
response.error = "TimeoutException";
|
||||
|
||||
@@ -99,8 +99,16 @@ class BarcodeHandler {
|
||||
|
||||
Map<String, dynamic> data = response.asMap();
|
||||
|
||||
// No usable response (connection failure, timeout, certificate error,
|
||||
// or a 5xx server error). The API layer has already shown an error
|
||||
// to the user, and reported any unexpected server errors to sentry.
|
||||
if (!response.isValid()) {
|
||||
barcodeFailureTone();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle strange response from the server
|
||||
if (!response.isValid() || !response.isMap()) {
|
||||
if (!response.isMap()) {
|
||||
await onBarcodeUnknown({});
|
||||
|
||||
showSnackIcon(L10().serverError, success: false);
|
||||
|
||||
@@ -50,11 +50,17 @@ class InvenTreeStatusCode {
|
||||
|
||||
APIResponse response = await InvenTreeAPI().get(URL);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
Map<String, dynamic> results = response.data as Map<String, dynamic>;
|
||||
// Note: the request may be interrupted (e.g. by a disconnect) after the
|
||||
// status code is received, so the response data must be type-checked
|
||||
if (response.statusCode == 200 && response.error.isEmpty) {
|
||||
final dynamic results = response.data;
|
||||
|
||||
if (results.containsKey("values")) {
|
||||
data = results["values"] as Map<String, dynamic>;
|
||||
if (results is Map<String, dynamic>) {
|
||||
final dynamic values = results["values"];
|
||||
|
||||
if (values is Map<String, dynamic>) {
|
||||
data = values;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,11 @@ void main() {
|
||||
|
||||
// Check debug output
|
||||
debugContains("scanned: abc");
|
||||
debugContains("No match for barcode");
|
||||
debugContains("Server Error");
|
||||
debugContains("Scanned barcode data: 'abc'");
|
||||
|
||||
// No server is available, so the API layer reports the failure,
|
||||
// and the handler exits early (rather than treating it as an unknown barcode)
|
||||
debugContains("showServerError");
|
||||
expect(debugContains("No match for barcode", raiseAssert: false), isFalse);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user