mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 13:36:50 +00:00
Refactor "uploadFile" function
- Error catching!
This commit is contained in:
parent
3f63f1e8a7
commit
e108598557
78
lib/api.dart
78
lib/api.dart
@ -42,6 +42,14 @@ class APIResponse {
|
|||||||
|
|
||||||
// Request is "valid" if a statusCode was returned
|
// Request is "valid" if a statusCode was returned
|
||||||
bool isValid() => (statusCode >= 0) && (statusCode < 500);
|
bool isValid() => (statusCode >= 0) && (statusCode < 500);
|
||||||
|
|
||||||
|
bool successful() => (statusCode >= 200) && (statusCode < 300);
|
||||||
|
|
||||||
|
bool redirected() => (statusCode >= 300) && (statusCode < 400);
|
||||||
|
|
||||||
|
bool clientError() => (statusCode >= 400) && (statusCode < 500);
|
||||||
|
|
||||||
|
bool serverError() => (statusCode >= 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -308,8 +316,6 @@ class InvenTreeAPI {
|
|||||||
// Clear the existing token value
|
// Clear the existing token value
|
||||||
_token = "";
|
_token = "";
|
||||||
|
|
||||||
print("Requesting token from server");
|
|
||||||
|
|
||||||
response = await get(_URL_GET_TOKEN);
|
response = await get(_URL_GET_TOKEN);
|
||||||
|
|
||||||
// Invalid response
|
// Invalid response
|
||||||
@ -563,9 +569,8 @@ class InvenTreeAPI {
|
|||||||
/*
|
/*
|
||||||
* Upload a file to the given URL
|
* Upload a file to the given URL
|
||||||
*/
|
*/
|
||||||
Future<http.StreamedResponse> uploadFile(String url, File f,
|
Future<APIResponse> uploadFile(String url, File f,
|
||||||
{String name = "attachment", String method="POST", Map<String, String>? fields}) async {
|
{String name = "attachment", String method="POST", Map<String, String>? fields}) async {
|
||||||
|
|
||||||
var _url = makeApiUrl(url);
|
var _url = makeApiUrl(url);
|
||||||
|
|
||||||
var request = http.MultipartRequest(method, Uri.parse(_url));
|
var request = http.MultipartRequest(method, Uri.parse(_url));
|
||||||
@ -582,25 +587,63 @@ class InvenTreeAPI {
|
|||||||
|
|
||||||
request.files.add(_file);
|
request.files.add(_file);
|
||||||
|
|
||||||
var response = await request.send();
|
APIResponse response = APIResponse(
|
||||||
|
url: url,
|
||||||
|
method: method,
|
||||||
|
);
|
||||||
|
|
||||||
if (response.statusCode >= 500) {
|
String jsondata = "";
|
||||||
// Server error
|
|
||||||
|
try {
|
||||||
|
var httpResponse = await request.send().timeout(Duration(seconds: 120));
|
||||||
|
|
||||||
|
response.statusCode = httpResponse.statusCode;
|
||||||
|
|
||||||
|
jsondata = await httpResponse.stream.bytesToString();
|
||||||
|
|
||||||
|
response.data = json.decode(jsondata);
|
||||||
|
|
||||||
|
// Report a server-side error
|
||||||
if (response.statusCode >= 500) {
|
if (response.statusCode >= 500) {
|
||||||
|
|
||||||
var data = await response.stream.bytesToString();
|
|
||||||
|
|
||||||
sentryReportMessage(
|
sentryReportMessage(
|
||||||
"Server error on file upload",
|
"Server error in uploadFile()",
|
||||||
context: {
|
context: {
|
||||||
"url": _url,
|
"url": url,
|
||||||
"statusCode": "${response.statusCode}",
|
"method": request.method,
|
||||||
"response": response.toString(),
|
"name": name,
|
||||||
"request": request.fields.toString(),
|
"statusCode": response.statusCode.toString(),
|
||||||
"data": data,
|
"requestHeaders": request.headers.toString(),
|
||||||
|
"responseHeaders": httpResponse.headers.toString(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} on SocketException catch (error) {
|
||||||
|
showServerError(L10().connectionRefused, error.toString());
|
||||||
|
response.error = "SocketException";
|
||||||
|
response.errorDetail = error.toString();
|
||||||
|
} on FormatException {
|
||||||
|
showServerError(
|
||||||
|
L10().formatException,
|
||||||
|
L10().formatExceptionJson + ":\n${jsondata}"
|
||||||
|
);
|
||||||
|
|
||||||
|
sentryReportMessage(
|
||||||
|
"Error decoding JSON response from server",
|
||||||
|
context: {
|
||||||
|
"url": url,
|
||||||
|
"statusCode": response.statusCode.toString(),
|
||||||
|
"data": jsondata,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
} on TimeoutException {
|
||||||
|
showTimeoutError();
|
||||||
|
response.error = "TimeoutException";
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
showServerError(L10().serverError, error.toString());
|
||||||
|
sentryReportError(error, stackTrace);
|
||||||
|
response.error = "UnknownError";
|
||||||
|
response.errorDetail = error.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@ -827,9 +870,6 @@ class InvenTreeAPI {
|
|||||||
return data ?? {};
|
return data ?? {};
|
||||||
} on FormatException {
|
} on FormatException {
|
||||||
|
|
||||||
print("JSON format exception!");
|
|
||||||
print("${body}");
|
|
||||||
|
|
||||||
sentryReportMessage(
|
sentryReportMessage(
|
||||||
"Error decoding JSON response from server",
|
"Error decoding JSON response from server",
|
||||||
context: {
|
context: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user