From b8497d3c20ac686160b7f8ba00f842424642c464 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 27 Feb 2021 22:38:36 +1100 Subject: [PATCH] Catch JSON decode errors when trying to extract user roles from an older server --- lib/api.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index bbbfb391..c7600032 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -359,6 +359,8 @@ class InvenTreeAPI { roles.clear(); + print("Requesting user role data"); + // Next we request the permissions assigned to the current user // Note: 2021-02-27 this "roles" feature for the API was just introduced. // Any 'older' version of the server allows any API method for any logged in user! @@ -369,15 +371,23 @@ class InvenTreeAPI { print(error); }).then((response) { + print("Response status: ${response.statusCode}"); + if (response.statusCode == 200) { // Convert response to JSON representation - var data = json.decode(response.body); - if (data.containsKey('roles')) { + try { + var data = json.decode(response.body); + if (data.containsKey('roles')) { // Save a local copy of the user roles roles = data['roles']; } + } + on FormatException { + // Old server has re-directed away from the API + } + } else { } }); }