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):
|
def post(self, request, *args, **kwargs):
|
||||||
|
|
||||||
self.get_items(request)
|
|
||||||
|
|
||||||
data = request.data
|
data = request.data
|
||||||
|
|
||||||
try:
|
try:
|
||||||
location = StockLocation.objects.get(pk=data.get('location', None))
|
location = StockLocation.objects.get(pk=data.get('location', None))
|
||||||
except (ValueError, StockLocation.DoesNotExist):
|
except (ValueError, StockLocation.DoesNotExist):
|
||||||
raise ValidationError({'location': 'Valid location must be specified'})
|
raise ValidationError({'location': [_('Valid location must be specified')]})
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
|
|
||||||
|
self.get_items(request)
|
||||||
|
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
|
|
||||||
# If quantity is not specified, move the entire stock
|
# If quantity is not specified, move the entire stock
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
{% load i18n %}
|
||||||
|
{% load inventree_extras %}
|
||||||
|
|
||||||
var jQuery = window.$;
|
var jQuery = window.$;
|
||||||
|
|
||||||
// using jQuery
|
// using jQuery
|
||||||
@ -139,3 +142,48 @@ function inventreeDelete(url, options={}) {
|
|||||||
inventreePut(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);
|
||||||
|
}
|
@ -423,9 +423,7 @@ function constructFormBody(fields, options) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var f = constructField(name, field, options);
|
html += constructField(name, field, options);
|
||||||
|
|
||||||
html += f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Dynamically create the modals,
|
// TODO: Dynamically create the modals,
|
||||||
@ -599,47 +597,9 @@ function submitFormData(fields, options) {
|
|||||||
case 400: // Bad request
|
case 400: // Bad request
|
||||||
handleFormErrors(xhr.responseJSON, fields, options);
|
handleFormErrors(xhr.responseJSON, fields, options);
|
||||||
break;
|
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:
|
default:
|
||||||
$(options.modal).modal('hide');
|
$(options.modal).modal('hide');
|
||||||
|
showApiError(xhr);
|
||||||
showAlertDialog('{% trans "Error requesting form data" %}', renderErrorMessage(xhr));
|
|
||||||
|
|
||||||
console.log(`WARNING: Unhandled response code - ${xhr.status}`);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,12 +39,13 @@ function createNewModal(options={}) {
|
|||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class='modal-form-content-wrapper'>
|
<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'>
|
<div id='non-field-errors'>
|
||||||
<!-- Form error messages go here -->
|
<!-- Form error messages go here -->
|
||||||
</div>
|
</div>
|
||||||
|
<div id='pre-form-content'>
|
||||||
|
<!-- Content can be inserted here *before* the form fields -->
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id='form-content' class='modal-form-content'>
|
<div id='form-content' class='modal-form-content'>
|
||||||
<!-- Form content will be injected here-->
|
<!-- Form content will be injected here-->
|
||||||
</div>
|
</div>
|
||||||
|
@ -255,6 +255,28 @@ function adjustStock(items, options={}) {
|
|||||||
if (options.onSuccess) {
|
if (options.onSuccess) {
|
||||||
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