mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor showApiError() function
This commit is contained in:
parent
0c41cc7c77
commit
e04828214a
@ -244,17 +244,17 @@ class StockTransfer(StockAdjust):
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
self.get_items(request)
|
||||
|
||||
data = request.data
|
||||
|
||||
try:
|
||||
location = StockLocation.objects.get(pk=data.get('location', None))
|
||||
except (ValueError, StockLocation.DoesNotExist):
|
||||
raise ValidationError({'location': 'Valid location must be specified'})
|
||||
raise ValidationError({'location': [_('Valid location must be specified')]})
|
||||
|
||||
n = 0
|
||||
|
||||
self.get_items(request)
|
||||
|
||||
for item in self.items:
|
||||
|
||||
# If quantity is not specified, move the entire stock
|
||||
|
@ -1,3 +1,6 @@
|
||||
{% load i18n %}
|
||||
{% load inventree_extras %}
|
||||
|
||||
var jQuery = window.$;
|
||||
|
||||
// using jQuery
|
||||
@ -138,4 +141,49 @@ function inventreeDelete(url, options={}) {
|
||||
|
||||
inventreePut(url, {}, options);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function showApiError(xhr) {
|
||||
|
||||
var title = null;
|
||||
var message = null;
|
||||
|
||||
switch (xhr.status) {
|
||||
case 0: // No response
|
||||
title = '{% trans "No Response" %}';
|
||||
message = '{% trans "No response from the InvenTree server" %}';
|
||||
break;
|
||||
case 400: // Bad request
|
||||
// Note: Normally error code 400 is handled separately,
|
||||
// and should now be shown here!
|
||||
title = '{% trans "Error 400: Bad request" %}';
|
||||
message = '{% trans "API request returned error code 400" %}';
|
||||
break;
|
||||
case 401: // Not authenticated
|
||||
title = '{% trans "Error 401: Not Authenticated" %}';
|
||||
message = '{% trans "Authentication credentials not supplied" %}';
|
||||
break;
|
||||
case 403: // Permission denied
|
||||
title = '{% trans "Error 403: Permission Denied" %}';
|
||||
message = '{% trans "You do not have the required permissions to access this function" %}';
|
||||
break;
|
||||
case 404: // Resource not found
|
||||
title = '{% trans "Error 404: Resource Not Found" %}';
|
||||
message = '{% trans "The requested resource could not be located on the server" %}';
|
||||
break;
|
||||
case 408: // Timeout
|
||||
title = '{% trans "Error 408: Timeout" %}';
|
||||
message = '{% trans "Connection timeout while requesting data from server" %}';
|
||||
break;
|
||||
default:
|
||||
title = '{% trans "Unhandled Error Code" %}';
|
||||
message = `{% trans "Error code" %}: ${xhr.status}`;
|
||||
break;
|
||||
}
|
||||
|
||||
message += "<hr>";
|
||||
message += renderErrorMessage(xhr);
|
||||
|
||||
showAlertDialog(title, message);
|
||||
}
|
@ -422,10 +422,8 @@ function constructFormBody(fields, options) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
var f = constructField(name, field, options);
|
||||
|
||||
html += f;
|
||||
html += constructField(name, field, options);
|
||||
}
|
||||
|
||||
// TODO: Dynamically create the modals,
|
||||
@ -599,47 +597,9 @@ function submitFormData(fields, options) {
|
||||
case 400: // Bad request
|
||||
handleFormErrors(xhr.responseJSON, fields, options);
|
||||
break;
|
||||
case 0: // No response
|
||||
$(options.modal).modal('hide');
|
||||
showAlertDialog(
|
||||
'{% trans "No Response" %}',
|
||||
'{% trans "No response from the InvenTree server" %}',
|
||||
);
|
||||
break;
|
||||
case 401: // Not authenticated
|
||||
$(options.modal).modal('hide');
|
||||
showAlertDialog(
|
||||
'{% trans "Error 401: Not Authenticated" %}',
|
||||
'{% trans "Authentication credentials not supplied" %}',
|
||||
);
|
||||
break;
|
||||
case 403: // Permission denied
|
||||
$(options.modal).modal('hide');
|
||||
showAlertDialog(
|
||||
'{% trans "Error 403: Permission Denied" %}',
|
||||
'{% trans "You do not have the required permissions to access this function" %}',
|
||||
);
|
||||
break;
|
||||
case 404: // Resource not found
|
||||
$(options.modal).modal('hide');
|
||||
showAlertDialog(
|
||||
'{% trans "Error 404: Resource Not Found" %}',
|
||||
'{% trans "The requested resource could not be located on the server" %}',
|
||||
);
|
||||
break;
|
||||
case 408: // Timeout
|
||||
$(options.modal).modal('hide');
|
||||
showAlertDialog(
|
||||
'{% trans "Error 408: Timeout" %}',
|
||||
'{% trans "Connection timeout while requesting data from server" %}',
|
||||
);
|
||||
break;
|
||||
default:
|
||||
$(options.modal).modal('hide');
|
||||
|
||||
showAlertDialog('{% trans "Error requesting form data" %}', renderErrorMessage(xhr));
|
||||
|
||||
console.log(`WARNING: Unhandled response code - ${xhr.status}`);
|
||||
showApiError(xhr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -39,12 +39,13 @@ function createNewModal(options={}) {
|
||||
</h3>
|
||||
</div>
|
||||
<div class='modal-form-content-wrapper'>
|
||||
<div id='pre-form-content'>
|
||||
<!-- Content can be inserted here *before* the form fields -->
|
||||
</div>
|
||||
<div id='non-field-errors'>
|
||||
<!-- Form error messages go here -->
|
||||
</div>
|
||||
<div id='pre-form-content'>
|
||||
<!-- Content can be inserted here *before* the form fields -->
|
||||
</div>
|
||||
|
||||
<div id='form-content' class='modal-form-content'>
|
||||
<!-- Form content will be injected here-->
|
||||
</div>
|
||||
|
@ -255,6 +255,28 @@ function adjustStock(items, options={}) {
|
||||
if (options.onSuccess) {
|
||||
options.onSuccess();
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
switch (xhr.status) {
|
||||
case 400:
|
||||
console.log('400 bad request');
|
||||
console.log(xhr.responseJSON);
|
||||
|
||||
// Handle errors for standard fields
|
||||
handleFormErrors(
|
||||
xhr.responseJSON,
|
||||
extraFields,
|
||||
{
|
||||
modal: modal,
|
||||
}
|
||||
)
|
||||
|
||||
break;
|
||||
default:
|
||||
$(modal).modal('hide');
|
||||
showApiError(xhr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user