diff --git a/lib/api.dart b/lib/api.dart index 13acf7c9..adf69800 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -42,7 +42,7 @@ class APIResponse { this.method = "", this.statusCode = -1, this.error = "", - this.data = const {}, + this.data = const {}, }); 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"; diff --git a/lib/barcode/handler.dart b/lib/barcode/handler.dart index 3b9a72ca..18bfadb8 100644 --- a/lib/barcode/handler.dart +++ b/lib/barcode/handler.dart @@ -99,8 +99,16 @@ class BarcodeHandler { Map 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); diff --git a/lib/inventree/status_codes.dart b/lib/inventree/status_codes.dart index fb3b5a1f..41a5462b 100644 --- a/lib/inventree/status_codes.dart +++ b/lib/inventree/status_codes.dart @@ -50,11 +50,17 @@ class InvenTreeStatusCode { APIResponse response = await InvenTreeAPI().get(URL); - if (response.statusCode == 200) { - Map results = response.data as Map; + // 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; + if (results is Map) { + final dynamic values = results["values"]; + + if (values is Map) { + data = values; + } } } } diff --git a/test/wedge_scanner_test.dart b/test/wedge_scanner_test.dart index bd98872f..51379f86 100644 --- a/test/wedge_scanner_test.dart +++ b/test/wedge_scanner_test.dart @@ -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); }); }