implement error types for disposal patterns

This commit is contained in:
Hazelnoot 2025-11-09 00:51:52 -05:00
parent b013649a41
commit c4866c9712

View file

@ -0,0 +1,40 @@
/*
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
/**
* Common base class for DisposedError and DisposingError - please use only for catch() blocks.
*/
export abstract class DisposeError extends Error {
public readonly source: string | undefined;
protected constructor(opts?: { source?: string, message?: string }) {
super();
this.source = opts?.source;
}
}
/**
* Thrown when an attempt is made to use an object that has been disposed.
*/
export class DisposedError extends DisposeError {
constructor(opts?: { source?: string, message?: string }) {
super({
source: opts?.source,
message: opts?.message ?? `${opts?.source ?? 'Object'} has been disposed`,
});
}
}
/**
* Thrown when an object is use begins disposing.
*/
export class DisposingError extends DisposeError {
constructor(opts?: { source?: string, message?: string }) {
super({
source: opts?.source,
message: opts?.message ?? `${opts?.source ?? 'Object'} is being disposed`,
});
}
}