|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { UMB_AUTH_CONTEXT } from '../auth/index.js'; |
| 3 | +import { isApiError, isCancelError, isCancelablePromise } from './apiTypeValidators.function.js'; |
1 | 4 | import { html } from '@umbraco-cms/backoffice/external/lit';
|
2 | 5 | import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
3 | 6 | import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
|
4 | 7 | import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
|
5 |
| -/* eslint-disable @typescript-eslint/no-explicit-any */ |
6 |
| -import { UMB_AUTH_CONTEXT } from '../auth/index.js'; |
7 |
| -import { isApiError, isCancelError, isCancelablePromise } from './apiTypeValidators.function.js'; |
8 | 8 | import { UMB_NOTIFICATION_CONTEXT, type UmbNotificationOptions } from '@umbraco-cms/backoffice/notification';
|
9 | 9 | import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
|
10 | 10 |
|
| 11 | +export type ErrorMessageText = { category: string; messages: string[] }; |
| 12 | + |
11 | 13 | export class UmbResourceController extends UmbControllerBase {
|
12 | 14 | #promise: Promise<any>;
|
13 | 15 |
|
@@ -37,12 +39,15 @@ export class UmbResourceController extends UmbControllerBase {
|
37 | 39 | this.cancel();
|
38 | 40 | }
|
39 | 41 |
|
40 |
| - #buildApiErrorMessage(error: any) { |
41 |
| - const entries: Array<Record<string, any>> = []; |
| 42 | + #buildApiErrorMessage(error: ErrorMessageText) { |
| 43 | + if (!error) return undefined; |
| 44 | + if (typeof error !== 'object') return undefined; |
42 | 45 |
|
43 |
| - Object.entries(error).forEach(([category, message]) => { |
44 |
| - entries.push({ category, messages: message as string[] }); |
| 46 | + const entries: Array<Record<string, any>> = []; |
| 47 | + Object.entries(error).forEach(([property, message]) => { |
| 48 | + entries.push({ property, messages: Array.isArray(message) ? message : [message] }); |
45 | 49 | });
|
| 50 | + |
46 | 51 | const template = html` ${entries.map((e) => e.messages.map((msg: string) => html`<div>${msg}</div>`))}`;
|
47 | 52 |
|
48 | 53 | return template;
|
@@ -141,7 +146,7 @@ export class UmbResourceController extends UmbControllerBase {
|
141 | 146 | default:
|
142 | 147 | // Other errors
|
143 | 148 | if (this.#notificationContext) {
|
144 |
| - const message = error.body?.errors ? this.#buildApiErrorMessage(error.body.errors) : undefined; |
| 149 | + const message = this.#buildApiErrorMessage(error?.body?.errors); |
145 | 150 | this.#notificationContext.peek('danger', {
|
146 | 151 | data: {
|
147 | 152 | headline: error.body?.title ?? error.name ?? 'Server Error',
|
|
0 commit comments