diff --git a/packages/backend/src/misc/errors/AbortedError.ts b/packages/backend/src/misc/errors/AbortedError.ts new file mode 100644 index 0000000000..99a0581fc4 --- /dev/null +++ b/packages/backend/src/misc/errors/AbortedError.ts @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { renderInlineError } from '@/misc/render-inline-error.js'; + +/** + * Throw when an operation is unexpectedly aborted. + */ +export class AbortedError extends Error { + // Fix the error name in stack traces - https://stackoverflow.com/a/71573071 + override name = this.constructor.name; + + constructor(signal: AbortSignal, message?: string, options?: Omit) { + super( + `Operation aborted: ${message ?? renderInlineError(signal.reason)}`, + { + ...(options ?? {}), + cause: signal.reason, + }, + ); + } +}