Skip to content

Commit 30bce63

Browse files
committed
feat: check if the error body is a ProblemDetails object and if we should cancel our default notifications
1 parent 86fdeb5 commit 30bce63

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
66
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
77
import { UMB_NOTIFICATION_CONTEXT, type UmbNotificationOptions } from '@umbraco-cms/backoffice/notification';
88
import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
9+
import type { ProblemDetails } from '@umbraco-cms/backoffice/external/backend-api';
910

1011
export class UmbResourceController extends UmbControllerBase {
1112
#promise: Promise<any>;
@@ -57,8 +58,8 @@ export class UmbResourceController extends UmbControllerBase {
5758
* If the executor function throws an error, then show the details in a notification.
5859
*/
5960
async tryExecuteAndNotify<T>(options?: UmbNotificationOptions): Promise<UmbDataSourceResponse<T>> {
60-
const { data, error: _error } = await UmbResourceController.tryExecute<T>(this.#promise);
61-
const error: any = _error;
61+
const { data, error } = await UmbResourceController.tryExecute<T>(this.#promise);
62+
6263
if (error) {
6364
/**
6465
* Determine if we want to show a notification or just log the error to the console.
@@ -71,18 +72,33 @@ export class UmbResourceController extends UmbControllerBase {
7172
} else {
7273
console.group('ApiError caught in UmbResourceController');
7374
console.error('Request failed', error.request);
74-
console.error('ProblemDetails', error.body);
75+
console.error('Request body', error.body);
7576
console.error('Error', error);
7677

78+
let problemDetails: ProblemDetails | null = null;
79+
7780
// ApiError - body could hold a ProblemDetails from the server
7881
if (typeof error.body !== 'undefined' && !!error.body) {
7982
try {
80-
(error as any).body = typeof error.body === 'string' ? JSON.parse(error.body) : error.body;
83+
(error as any).body = problemDetails = typeof error.body === 'string' ? JSON.parse(error.body) : error.body;
8184
} catch (e) {
8285
console.error('Error parsing error body (expected JSON)', e);
8386
}
8487
}
8588

89+
/**
90+
* Check if the operation status ends with `ByNotification` and if so, don't show a notification
91+
* This is a special case where the operation was cancelled by the server and the client gets a notification header instead.
92+
*/
93+
let isCancelledByNotification = false;
94+
if (
95+
problemDetails?.operationStatus &&
96+
typeof problemDetails.operationStatus === 'string' &&
97+
problemDetails.operationStatus.endsWith('ByNotification')
98+
) {
99+
isCancelledByNotification = true;
100+
}
101+
86102
// Go through the error status codes and act accordingly
87103
switch (error.status ?? 0) {
88104
case 401: {
@@ -103,14 +119,14 @@ export class UmbResourceController extends UmbControllerBase {
103119
case 500:
104120
// Server Error
105121

106-
if (this.#notificationContext) {
107-
let headline = error.body?.title ?? error.name ?? 'Server Error';
122+
if (!isCancelledByNotification && this.#notificationContext) {
123+
let headline = problemDetails?.title ?? error.name ?? 'Server Error';
108124
let message = 'A fatal server error occurred. If this continues, please reach out to your administrator.';
109125

110126
// Special handling for ObjectCacheAppCache corruption errors, which we are investigating
111127
if (
112-
error.body?.detail?.includes('ObjectCacheAppCache') ||
113-
error.body?.detail?.includes('Umbraco.Cms.Infrastructure.Scoping.Scope.DisposeLastScope()')
128+
problemDetails?.detail?.includes('ObjectCacheAppCache') ||
129+
problemDetails?.detail?.includes('Umbraco.Cms.Infrastructure.Scoping.Scope.DisposeLastScope()')
114130
) {
115131
headline = 'Please restart the server';
116132
message =
@@ -128,12 +144,14 @@ export class UmbResourceController extends UmbControllerBase {
128144
break;
129145
default:
130146
// Other errors
131-
if (this.#notificationContext) {
147+
if (!isCancelledByNotification && this.#notificationContext) {
132148
this.#notificationContext.peek('danger', {
133149
data: {
134-
headline: error.body?.title ?? error.name ?? 'Server Error',
135-
message: error.body?.detail ?? error.message ?? 'Something went wrong',
136-
structuredList: error.body.errors,
150+
headline: problemDetails?.title ?? error.name ?? 'Server Error',
151+
message: problemDetails?.detail ?? error.message ?? 'Something went wrong',
152+
structuredList: problemDetails?.errors
153+
? (problemDetails.errors as Record<string, Array<unknown>>)
154+
: undefined,
137155
},
138156
...options,
139157
});

0 commit comments

Comments
 (0)