2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Better error handling for URL parsing

This commit is contained in:
Oliver 2021-07-13 08:01:45 +10:00
parent 8b5bd0f213
commit 374a387355

View File

@ -710,7 +710,12 @@ class InvenTreeAPI {
var client = createClient(true); var client = createClient(true);
final uri = Uri.parse(_url); Uri? uri = Uri.tryParse(_url);
if (uri == null) {
showServerError(L10().invalidHost, L10().invalidHostDetails);
return null;
}
// Check for invalid host // Check for invalid host
if (uri.host.isEmpty) { if (uri.host.isEmpty) {
@ -718,32 +723,55 @@ class InvenTreeAPI {
return null; return null;
} }
// Open a connection HttpClientRequest? request;
HttpClientRequest? request = await client.getUrl(uri)
.timeout(Duration(seconds: 10))
.catchError((error) {
print("GET request returned error");
print("URL: ${uri}");
print("Error: ${error.toString()}");
var ctx = OneContext().context; try {
// Open a connection
request = await client.getUrl(uri)
.timeout(Duration(seconds: 10))
.catchError((error) {
print("GET request returned error");
print("URL: ${uri}");
print("Error: ${error.toString()}");
if (error is SocketException) { var ctx = OneContext().context;
if (error is SocketException) {
showServerError(
L10().connectionRefused,
error.toString()
);
} else if (error is TimeoutException) {
showTimeoutError();
} else {
showServerError(
L10().serverError,
error.toString()
);
}
return null;
});
} catch (error) {
if (error is FormatException) {
showServerError(
L10().invalidHost,
L10().invalidHostDetails)
;
} else if (error is SocketException) {
showServerError( showServerError(
L10().connectionRefused, L10().connectionRefused,
error.toString() error.toString()
); );
} else if (error is TimeoutException) {
showTimeoutError();
} else { } else {
showServerError( showServerError(
L10().serverError, L10().serverError,
error.toString() error.toString()
); );
} }
return null; return null;
}); }
if (request == null) { if (request == null) {
return null; return null;