mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
Add better error handling for API
This commit is contained in:
parent
0f472d448b
commit
e04dadcd2f
13
lib/api.dart
13
lib/api.dart
@ -182,15 +182,22 @@ class InvenTreeAPI {
|
|||||||
var response = await get("").timeout(Duration(seconds: 10)).catchError((error) {
|
var response = await get("").timeout(Duration(seconds: 10)).catchError((error) {
|
||||||
|
|
||||||
if (error is SocketException) {
|
if (error is SocketException) {
|
||||||
errorMessage = "Could not connect to server.";
|
print("Could not connect to server");
|
||||||
print(errorMessage);
|
return null;
|
||||||
throw errorMessage;
|
} else if (error is TimeoutException) {
|
||||||
|
print("Server timeout");
|
||||||
|
return null;
|
||||||
} else {
|
} else {
|
||||||
// Unknown error type, re-throw error
|
// Unknown error type, re-throw error
|
||||||
|
print("Unknown error: ${error.toString()}");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (response == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200) {
|
||||||
print("Invalid status code: " + response.statusCode.toString());
|
print("Invalid status code: " + response.statusCode.toString());
|
||||||
return false;
|
return false;
|
||||||
|
@ -134,22 +134,6 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
onConnectFailure("Could not connect to server");
|
onConnectFailure("Could not connect to server");
|
||||||
}
|
}
|
||||||
|
|
||||||
}).catchError((e) {
|
|
||||||
|
|
||||||
String fault = "Connection error";
|
|
||||||
|
|
||||||
_serverConnection = false;
|
|
||||||
_serverStatusColor = Color.fromARGB(255, 250, 50, 50);
|
|
||||||
|
|
||||||
_serverStatus = "Error connecting to $_serverAddress";
|
|
||||||
|
|
||||||
if (e is TimeoutException) {
|
|
||||||
fault = "Timeout: No response from server";
|
|
||||||
} else {
|
|
||||||
fault = e.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
onConnectFailure(fault);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update widget state
|
// Update widget state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user