diff --git a/assets/release_notes.md b/assets/release_notes.md index 030b6d07..24036c66 100644 --- a/assets/release_notes.md +++ b/assets/release_notes.md @@ -5,6 +5,7 @@ --- - Fixes certificate issues connecting to HTTPs server +- Fixes some app crash bugs - Bug fixes for various API calls - UI cleanup diff --git a/lib/api.dart b/lib/api.dart index a755140a..b95b7616 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -209,9 +209,6 @@ class InvenTreeAPI { return false; } - - print("Response from server: ${response}"); - // We expect certain response from the server if (!response.containsKey("server") || !response.containsKey("version") || !response.containsKey("instance")) { @@ -395,7 +392,37 @@ class InvenTreeAPI { var client = createClient(true); - HttpClientRequest request = await client.patchUrl(Uri.parse(_url)); + // Open a connection to the server + HttpClientRequest request = await client.patchUrl(Uri.parse(_url)) + .timeout(Duration(seconds: 10)) + .catchError((error) { + print("PATCH request return error"); + print("URL: ${_url}"); + print("Error: ${error.toString()}"); + + var ctx = OneContext().context; + + if (error is SocketException) { + showServerError( + I18N.of(ctx).connectionRefused, + error.toString(), + ); + } else if (error is TimeoutException) { + showTimeoutError(ctx); + } else { + showServerError( + I18N.of(ctx).serverError, + error.toString() + ); + } + + return null; + }); + + // Request could not be made + if (request = null) { + return null; + } var data = json.encode(body); @@ -485,7 +512,36 @@ class InvenTreeAPI { var client = createClient(true); - HttpClientRequest request = await client.postUrl(Uri.parse(_url)); + // Open a connection to the server + HttpClientRequest request = await client.postUrl(Uri.parse(_url)) + .timeout(Duration(seconds: 10)) + .catchError((error) { + print("POST request returned error"); + print("URL: ${_url}"); + print("Error: ${error.toString()}"); + + var ctx = OneContext().context; + + if (error is SocketException) { + showServerError( + I18N.of(ctx).connectionRefused, + error.toString() + ); + } else if (error is TimeoutException) { + showTimeoutError(ctx); + } else { + showServerError( + I18N.of(ctx).serverError, + error.toString() + ); + } + + return null; + }); + + if (request == null) { + return null; + } var data = json.encode(body); @@ -607,18 +663,47 @@ class InvenTreeAPI { _url = _url.substring(0, _url.length - 1); } - print("GET: " + _url); - var client = createClient(true); - HttpClientRequest request = await client.getUrl(Uri.parse(_url)); + // Open a connection + HttpClientRequest request = await client.getUrl(Uri.parse(_url)) + .timeout(Duration(seconds: 10)) + .catchError((error) { + print("GET request returned error"); + print("URL: ${_url}"); + print("Error: ${error.toString()}"); - // Set headers + var ctx = OneContext().context; + + if (error is SocketException) { + showServerError( + I18N.of(ctx).connectionRefused, + error.toString() + ); + } else if (error is TimeoutException) { + showTimeoutError(ctx); + } else { + showServerError( + I18N.of(ctx).serverError, + error.toString() + ); + } + + return null; + }); + + if (request == null) { + return null; + } + + // Set connection headers request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); + print("Attempting connection"); + HttpClientResponse response = await request.close() - .timeout(Duration(seconds: 30)) + .timeout(Duration(seconds: 10)) .catchError((error) { print("GET request returned error"); print("URL: ${_url}"); @@ -643,6 +728,8 @@ class InvenTreeAPI { return null; }); + print("got here"); + // A null response means something has gone wrong... if (response == null) { print("null response from GET ${_url}"); diff --git a/lib/widget/dialogs.dart b/lib/widget/dialogs.dart index 30a17259..e6120d53 100644 --- a/lib/widget/dialogs.dart +++ b/lib/widget/dialogs.dart @@ -177,19 +177,24 @@ Future showStatusCodeError(int status, {int expected = 200}) async { Future showTimeoutError(BuildContext context) async { - await showServerError(I18N.of(context).timeout, I18N.of(context).noResponse); + // Use OneContext as "sometimes" context is null here? + var ctx = OneContext().context; + + await showServerError(I18N.of(ctx).timeout, I18N.of(ctx).noResponse); } void showFormDialog(String title, {String acceptText, String cancelText, GlobalKey key, List fields, List actions, Function callback}) { BuildContext dialogContext; + var ctx = OneContext().context; + if (acceptText == null) { - acceptText = I18N.of(OneContext().context).save; + acceptText = I18N.of(ctx).save; } if (cancelText == null) { - cancelText = I18N.of(OneContext().context).cancel; + cancelText = I18N.of(ctx).cancel; } // Undefined actions = OK + Cancel diff --git a/pubspec.yaml b/pubspec.yaml index 85a091fa..a7b576ef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,7 +7,7 @@ description: InvenTree stock management # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 0.1.3+7 +version: 0.1.4+8 environment: sdk: ">=2.1.0 <3.0.0"