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
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

View File

@ -199,12 +199,13 @@ 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) => {
const errorMessage: ValidationError = await ex.json();
if (!errorMessage) return errorMessage;
if (errorMessage instanceof Error) {
throw errorMessage;
@ -223,17 +224,11 @@ export class Form<T> extends LitElement {
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 ("detail" in errorMessage) {
msg = errorMessage.detail;
}
}
// error is local or not from rest_framework
showMessage({