From bc125ffdc9bf2f4832997296bc8b9c0efec242c3 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Wed, 4 Jan 2023 10:23:22 +0100 Subject: [PATCH] scope static functions --- .../src/core/resources/resource.controller.ts | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts b/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts index 3a7b268f10..5668c41180 100644 --- a/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/resources/resource.controller.ts @@ -6,25 +6,6 @@ import { ApiError, CancelablePromise, ProblemDetails } from '@umbraco-cms/backen import { UmbNotificationOptions, UmbNotificationService } from 'src/core/notification'; import { UmbNotificationDefaultData } from 'src/core/notification/layouts/default'; -/** - * Extract the ProblemDetails object from an ApiError. - * - * This assumes that all ApiErrors contain a ProblemDetails object in their body. - */ -function toProblemDetails(error: unknown): ProblemDetails | undefined { - if (error instanceof ApiError) { - const errorDetails = error.body as ProblemDetails; - return errorDetails; - } else if (error instanceof Error) { - return { - title: error.name, - detail: error.message, - }; - } - - return undefined; -} - export class UmbResourceController extends UmbController { #promise: Promise; @@ -49,13 +30,32 @@ export class UmbResourceController extends UmbController { } /** - * Wrap the {execute} function in a try/catch block and return a tuple with the result and the error. + * Extract the ProblemDetails object from an ApiError. + * + * This assumes that all ApiErrors contain a ProblemDetails object in their body. */ - async tryExecute(): Promise<{ data?: T; error?: ProblemDetails }> { + static toProblemDetails(error: unknown): ProblemDetails | undefined { + if (error instanceof ApiError) { + const errorDetails = error.body as ProblemDetails; + return errorDetails; + } else if (error instanceof Error) { + return { + title: error.name, + detail: error.message, + }; + } + + return undefined; + } + + /** + * Base execute function with a try/catch block and return a tuple with the result and the error. + */ + static async tryExecute(promise: Promise): Promise<{ data?: T; error?: ProblemDetails }> { try { - return { data: await this.#promise }; + return { data: await promise }; } catch (e) { - return { error: toProblemDetails(e) }; + return { error: UmbResourceController.toProblemDetails(e) }; } } @@ -64,7 +64,7 @@ export class UmbResourceController extends UmbController { * If the executor function throws an error, then show the details in a notification. */ async tryExecuteAndNotify(options?: UmbNotificationOptions): Promise<{ data?: T; error?: ProblemDetails }> { - const { data, error } = await this.tryExecute(); + const { data, error } = await UmbResourceController.tryExecute(this.#promise); if (error) { const data: UmbNotificationDefaultData = {