Skip to content

Commit c3c442c

Browse files
committed
type checking
1 parent 028940d commit c3c442c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/packages/core/resources/resource.controller.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
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';
14
import { html } from '@umbraco-cms/backoffice/external/lit';
25
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
36
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
47
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';
88
import { UMB_NOTIFICATION_CONTEXT, type UmbNotificationOptions } from '@umbraco-cms/backoffice/notification';
99
import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
1010

11+
export type ErrorMessageText = { category: string; messages: string[] };
12+
1113
export class UmbResourceController extends UmbControllerBase {
1214
#promise: Promise<any>;
1315

@@ -37,12 +39,15 @@ export class UmbResourceController extends UmbControllerBase {
3739
this.cancel();
3840
}
3941

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

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] });
4549
});
50+
4651
const template = html` ${entries.map((e) => e.messages.map((msg: string) => html`<div>${msg}</div>`))}`;
4752

4853
return template;
@@ -141,7 +146,7 @@ export class UmbResourceController extends UmbControllerBase {
141146
default:
142147
// Other errors
143148
if (this.#notificationContext) {
144-
const message = error.body?.errors ? this.#buildApiErrorMessage(error.body.errors) : undefined;
149+
const message = this.#buildApiErrorMessage(error?.body?.errors);
145150
this.#notificationContext.peek('danger', {
146151
data: {
147152
headline: error.body?.title ?? error.name ?? 'Server Error',

0 commit comments

Comments
 (0)