web/elements: improve error handling on forms
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
93bdea3769
commit
d0bfb99859
|
@ -21580,6 +21580,7 @@ components:
|
|||
readOnly: true
|
||||
webhook_url:
|
||||
type: string
|
||||
format: uri
|
||||
webhook_mapping:
|
||||
type: string
|
||||
format: uuid
|
||||
|
@ -21609,6 +21610,7 @@ components:
|
|||
$ref: '#/components/schemas/NotificationTransportModeEnum'
|
||||
webhook_url:
|
||||
type: string
|
||||
format: uri
|
||||
webhook_mapping:
|
||||
type: string
|
||||
format: uuid
|
||||
|
@ -25808,6 +25810,7 @@ components:
|
|||
$ref: '#/components/schemas/NotificationTransportModeEnum'
|
||||
webhook_url:
|
||||
type: string
|
||||
format: uri
|
||||
webhook_mapping:
|
||||
type: string
|
||||
format: uuid
|
||||
|
|
|
@ -199,41 +199,36 @@ export class Form<T> extends LitElement {
|
|||
);
|
||||
return r;
|
||||
})
|
||||
.catch((ex: Response | Error) => {
|
||||
.catch(async (ex: Response | Error) => {
|
||||
if (ex instanceof Error) {
|
||||
throw ex;
|
||||
}
|
||||
let msg = ex.statusText;
|
||||
if (ex.status > 399 && ex.status < 500) {
|
||||
return ex.json().then((errorMessage: ValidationError) => {
|
||||
if (!errorMessage) return errorMessage;
|
||||
if (errorMessage instanceof Error) {
|
||||
throw errorMessage;
|
||||
const errorMessage: ValidationError = await ex.json();
|
||||
if (!errorMessage) return errorMessage;
|
||||
if (errorMessage instanceof Error) {
|
||||
throw errorMessage;
|
||||
}
|
||||
// assign all input-related errors to their elements
|
||||
const elements: PaperInputElement[] = ironForm._getSubmittableElements();
|
||||
elements.forEach((element) => {
|
||||
const elementName = element.name;
|
||||
if (!elementName) return;
|
||||
if (camelToSnake(elementName) in errorMessage) {
|
||||
element.errorMessage =
|
||||
errorMessage[camelToSnake(elementName)].join(", ");
|
||||
element.invalid = true;
|
||||
}
|
||||
// assign all input-related errors to their elements
|
||||
const elements: PaperInputElement[] = ironForm._getSubmittableElements();
|
||||
elements.forEach((element) => {
|
||||
const elementName = element.name;
|
||||
if (!elementName) return;
|
||||
if (camelToSnake(elementName) in errorMessage) {
|
||||
element.errorMessage =
|
||||
errorMessage[camelToSnake(elementName)].join(", ");
|
||||
element.invalid = true;
|
||||
}
|
||||
});
|
||||
if ("non_field_errors" in errorMessage) {
|
||||
this.nonFieldErrors = errorMessage["non_field_errors"];
|
||||
}
|
||||
throw new APIError(errorMessage);
|
||||
});
|
||||
}
|
||||
throw ex;
|
||||
})
|
||||
.catch((ex: Error) => {
|
||||
let msg = ex.toString();
|
||||
// Only change the message when we have `detail`.
|
||||
// Everything else is handled in the form.
|
||||
if (ex instanceof APIError && "detail" in ex.response) {
|
||||
msg = ex.response.detail;
|
||||
if ("non_field_errors" in errorMessage) {
|
||||
this.nonFieldErrors = errorMessage["non_field_errors"];
|
||||
}
|
||||
// Only change the message when we have `detail`.
|
||||
// Everything else is handled in the form.
|
||||
if ("detail" in errorMessage) {
|
||||
msg = errorMessage.detail;
|
||||
}
|
||||
}
|
||||
// error is local or not from rest_framework
|
||||
showMessage({
|
||||
|
|
Reference in a new issue