From 644a55d91ed9fe3bbf7327cd162fce44fa64dc92 Mon Sep 17 00:00:00 2001 From: yougotwill Date: Fri, 7 Feb 2025 16:18:30 +1100 Subject: [PATCH] fix: use correct class for prototype chain restoration in custom errors --- ts/session/utils/errors.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ts/session/utils/errors.ts b/ts/session/utils/errors.ts index 6e365ac8e..f20c088a9 100644 --- a/ts/session/utils/errors.ts +++ b/ts/session/utils/errors.ts @@ -67,12 +67,17 @@ export class HTTPError extends Error { } } +/** + * Base error class for all errors in the session module. + * + * @note if you make a custom error with a custom message, make sure to restore the prototype chain again using the new class prototype. + */ class BaseError extends Error { constructor(message: string) { super(message); this.name = this.constructor.name; - // restore prototype chain - Object.setPrototypeOf(this, SnodeResponseError.prototype); + // NOTE Restores prototype chain. Make sure to reference the new class prototype! + Object.setPrototypeOf(this, BaseError.prototype); } } @@ -85,12 +90,12 @@ export class InvalidMessage extends BaseError {} export class SnodeResponseError extends BaseError { constructor(message = 'sessionRpc could not talk to node') { super(message); + Object.setPrototypeOf(this, SnodeResponseError.prototype); } } -export class RetrieveDisplayNameError extends Error { +export class RetrieveDisplayNameError extends BaseError { constructor(message = 'failed to retrieve display name after setting it') { super(message); - // restore prototype chain - Object.setPrototypeOf(this, SnodeResponseError.prototype); + Object.setPrototypeOf(this, RetrieveDisplayNameError.prototype); } }