mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-30 22:46:49 +00:00
Catch some subtle errors (#202)
- Prevent API requests for invalid PK values - Perform checks for invalid PK values at multiple points - Change order of operations in StockDetail widget
This commit is contained in:
parent
61bacefd36
commit
e7c5186823
@ -7,6 +7,7 @@
|
||||
- Allow serial numbers to be specified when creating new stock items
|
||||
- Allow serial numbers to be edited for existing stock items
|
||||
- Allow app locale to be changed manually
|
||||
- Improved handling of certain errors
|
||||
|
||||
### 0.8.1 - August 2022
|
||||
---
|
||||
|
@ -276,6 +276,12 @@ class InvenTreeModel {
|
||||
/// Delete the instance on the remote server
|
||||
/// Returns true if the operation was successful, else false
|
||||
Future<bool> delete() async {
|
||||
|
||||
// Return if we do not have a valid pk
|
||||
if (pk < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = await api.delete(url);
|
||||
|
||||
if (!response.isValid() || response.data == null || (response.data is! Map)) {
|
||||
@ -303,18 +309,35 @@ class InvenTreeModel {
|
||||
*/
|
||||
Future<bool> reload() async {
|
||||
|
||||
// If we do not have a valid pk (for some reason), exit immediately
|
||||
if (pk < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = await api.get(url, params: defaultGetFilters(), expectedStatusCode: 200);
|
||||
|
||||
if (!response.isValid() || response.data == null || (response.data is! Map)) {
|
||||
// A valid response has been returned
|
||||
if (response.isValid() && response.statusCode == 200) {
|
||||
|
||||
reportModelError(
|
||||
"InvenTreeModel.reload() returned invalid response",
|
||||
response,
|
||||
context: {
|
||||
"pk": pk.toString(),
|
||||
}
|
||||
);
|
||||
// Returned data was not a valid JSON object
|
||||
if (response.data == null || response.data is! Map) {
|
||||
reportModelError(
|
||||
"InvenTreeModel.reload() returned invalid response",
|
||||
response,
|
||||
context: {
|
||||
"pk": pk.toString(),
|
||||
}
|
||||
);
|
||||
|
||||
showServerError(
|
||||
url,
|
||||
L10().serverError,
|
||||
L10().responseInvalid,
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
showServerError(
|
||||
url,
|
||||
L10().serverError,
|
||||
@ -322,7 +345,6 @@ class InvenTreeModel {
|
||||
);
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
lastReload = DateTime.now();
|
||||
@ -337,6 +359,13 @@ class InvenTreeModel {
|
||||
|
||||
var url = path.join(URL, pk.toString());
|
||||
|
||||
// Return if we do not have a valid pk
|
||||
if (pk < 0) {
|
||||
return APIResponse(
|
||||
url: url,
|
||||
);
|
||||
}
|
||||
|
||||
if (!url.endsWith("/")) {
|
||||
url += "/";
|
||||
}
|
||||
@ -396,6 +425,11 @@ class InvenTreeModel {
|
||||
}
|
||||
|
||||
Future<InvenTreeModel?> get(int pk, {Map<String, String> filters = const {}}) async {
|
||||
|
||||
if (pk < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getModel(pk.toString(), filters: filters);
|
||||
}
|
||||
|
||||
|
@ -109,13 +109,13 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
|
||||
@override
|
||||
Future<void> request(BuildContext context) async {
|
||||
|
||||
final bool result = await item.reload();
|
||||
|
||||
stockShowHistory = await InvenTreeSettingsManager().getValue(INV_STOCK_SHOW_HISTORY, false) as bool;
|
||||
|
||||
final bool result = item.pk > 0 && await item.reload();
|
||||
|
||||
// Could not load this stock item for some reason
|
||||
// Perhaps it has been depleted?
|
||||
if (!result || item.pk == -1) {
|
||||
if (!result) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,9 @@ class _StockNotesState extends RefreshableState<StockNotesWidget> {
|
||||
|
||||
@override
|
||||
Future<void> request(BuildContext context) async {
|
||||
await item.reload();
|
||||
if (item.pk > 0) {
|
||||
await item.reload();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
Loading…
x
Reference in New Issue
Block a user