From d4db92eadd2db3850780128c3dcad0d9ca3abd29 Mon Sep 17 00:00:00 2001 From: Joey Ballentine Date: Sun, 30 Jun 2024 14:21:12 -0400 Subject: [PATCH] Use new error type --- src/common/Backend.ts | 29 ++++++++++++++++++++-- src/renderer/contexts/ExecutionContext.tsx | 6 ++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/common/Backend.ts b/src/common/Backend.ts index 32a99089d..6eac0fcf4 100644 --- a/src/common/Backend.ts +++ b/src/common/Backend.ts @@ -1,4 +1,4 @@ -import axios, { AxiosRequestConfig } from 'axios'; +import axios, { AxiosRequestConfig, Cancel } from 'axios'; import { BackendJsonNode, Category, @@ -133,6 +133,31 @@ export class ServerError extends Error { return new ServerError(json.message, json.description, json.status); } } +export class CancelError extends Error { + type = 'cancel'; + + message: string; + + config: AxiosRequestConfig | undefined; + + constructor(message: string | undefined, config?: AxiosRequestConfig) { + super(); + this.message = message ?? 'The request was cancelled'; + this.config = config; + } + + static isCancel(cancel: unknown): cancel is CancelError { + if (typeof cancel !== 'object' || cancel === null) { + return false; + } + const obj = cancel as Record; + return typeof obj.message === 'string' && obj.type === 'cancel'; + } + + static fromCancel(cancelData: Cancel, config?: AxiosRequestConfig): CancelError { + return new CancelError(cancelData.message, config); + } +} /** * A wrapper to communicate with the backend. @@ -183,7 +208,7 @@ export class Backend { return error.response.data as T; } if (axios.isCancel(error)) { - return { type: 'success' } as T; + throw CancelError.fromCancel(error, error.config); } } if (ServerError.isJson(error)) { diff --git a/src/renderer/contexts/ExecutionContext.tsx b/src/renderer/contexts/ExecutionContext.tsx index df76ecdaa..6a2d367b8 100644 --- a/src/renderer/contexts/ExecutionContext.tsx +++ b/src/renderer/contexts/ExecutionContext.tsx @@ -8,6 +8,7 @@ import { BackendEventMap, BackendStatusResponse, BackendWorkerStatusResponse, + CancelError, } from '../../common/Backend'; import { EdgeData, NodeData, OutputId } from '../../common/common-types'; import { formatExecutionErrorMessage } from '../../common/formatExecutionErrorMessage'; @@ -478,7 +479,10 @@ export const ExecutionProvider = memo(({ children }: React.PropsWithChildren<{}> }); } } catch (err: unknown) { - if (!(err instanceof DOMException && err.name === 'AbortError')) { + if ( + !(err instanceof DOMException && err.name === 'AbortError') && + !CancelError.isCancel(err) + ) { sendAlert({ type: AlertType.ERROR, message: `An unexpected error occurred: ${String(err)}`,