Skip to content

Commit b6f9560

Browse files
committed
show correct api error msg
1 parent dc27f58 commit b6f9560

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { html } from '@umbraco-cms/backoffice/external/lit';
2+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
3+
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
4+
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
15
/* eslint-disable @typescript-eslint/no-explicit-any */
26
import { UMB_AUTH_CONTEXT } from '../auth/index.js';
37
import { isApiError, isCancelError, isCancelablePromise } from './apiTypeValidators.function.js';
48
import { UMB_NOTIFICATION_CONTEXT, type UmbNotificationOptions } from '@umbraco-cms/backoffice/notification';
5-
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
6-
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
7-
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
89
import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
910

1011
export class UmbResourceController extends UmbControllerBase {
@@ -36,6 +37,17 @@ export class UmbResourceController extends UmbControllerBase {
3637
this.cancel();
3738
}
3839

40+
#buildApiErrorMessage(error: any) {
41+
const entries: Array<Record<string, any>> = [];
42+
43+
Object.entries(error).forEach(([category, message]) => {
44+
entries.push({ category, messages: message as string[] });
45+
});
46+
const template = html` ${entries.map((e) => e.messages.map((msg: string) => html`<div>${msg}</div>`))}`;
47+
48+
return template;
49+
}
50+
3951
/**
4052
* Base execute function with a try/catch block and return a tuple with the result and the error.
4153
*/
@@ -129,10 +141,11 @@ export class UmbResourceController extends UmbControllerBase {
129141
default:
130142
// Other errors
131143
if (this.#notificationContext) {
144+
const message = error.body?.errors ? this.#buildApiErrorMessage(error.body.errors) : undefined;
132145
this.#notificationContext.peek('danger', {
133146
data: {
134147
headline: error.body?.title ?? error.name ?? 'Server Error',
135-
message: error.body?.detail ?? error.message ?? 'Something went wrong',
148+
message: message ?? error.body?.detail ?? error.message ?? 'Something went wrong',
136149
},
137150
...options,
138151
});

0 commit comments

Comments
 (0)