web/elements: improve error handling on forms

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-09-24 12:19:56 +02:00
parent 93bdea3769
commit d0bfb99859
2 changed files with 27 additions and 29 deletions

View File

@ -21580,6 +21580,7 @@ components:
readOnly: true readOnly: true
webhook_url: webhook_url:
type: string type: string
format: uri
webhook_mapping: webhook_mapping:
type: string type: string
format: uuid format: uuid
@ -21609,6 +21610,7 @@ components:
$ref: '#/components/schemas/NotificationTransportModeEnum' $ref: '#/components/schemas/NotificationTransportModeEnum'
webhook_url: webhook_url:
type: string type: string
format: uri
webhook_mapping: webhook_mapping:
type: string type: string
format: uuid format: uuid
@ -25808,6 +25810,7 @@ components:
$ref: '#/components/schemas/NotificationTransportModeEnum' $ref: '#/components/schemas/NotificationTransportModeEnum'
webhook_url: webhook_url:
type: string type: string
format: uri
webhook_mapping: webhook_mapping:
type: string type: string
format: uuid format: uuid

View File

@ -199,41 +199,36 @@ export class Form<T> extends LitElement {
); );
return r; return r;
}) })
.catch((ex: Response | Error) => { .catch(async (ex: Response | Error) => {
if (ex instanceof Error) { if (ex instanceof Error) {
throw ex; throw ex;
} }
let msg = ex.statusText;
if (ex.status > 399 && ex.status < 500) { if (ex.status > 399 && ex.status < 500) {
return ex.json().then((errorMessage: ValidationError) => { const errorMessage: ValidationError = await ex.json();
if (!errorMessage) return errorMessage; if (!errorMessage) return errorMessage;
if (errorMessage instanceof Error) { if (errorMessage instanceof Error) {
throw errorMessage; 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);
}); });
} if ("non_field_errors" in errorMessage) {
throw ex; this.nonFieldErrors = errorMessage["non_field_errors"];
}) }
.catch((ex: Error) => { // Only change the message when we have `detail`.
let msg = ex.toString(); // Everything else is handled in the form.
// Only change the message when we have `detail`. if ("detail" in errorMessage) {
// Everything else is handled in the form. msg = errorMessage.detail;
if (ex instanceof APIError && "detail" in ex.response) { }
msg = ex.response.detail;
} }
// error is local or not from rest_framework // error is local or not from rest_framework
showMessage({ showMessage({