From fa90a3e7dcbe8c0952b0681be903dffb4b4c74c6 Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Mon, 3 Apr 2023 18:03:35 +0200 Subject: [PATCH] fix: implement custom ts error localy --- package-lock.json | 9 --------- package.json | 1 - src/provider/errors.ts | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3d907a7e3..074581eb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,6 @@ "lossless-json": "^2.0.8", "micro-starknet": "^0.2.1", "pako": "^2.0.4", - "ts-custom-error": "^3.3.1", "url-join": "^4.0.1" }, "devDependencies": { @@ -20077,14 +20076,6 @@ "node": ">=8" } }, - "node_modules/ts-custom-error": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/ts-custom-error/-/ts-custom-error-3.3.1.tgz", - "integrity": "sha512-5OX1tzOjxWEgsr/YEUWSuPrQ00deKLh6D7OTWcvNHm12/7QPyRh8SYpyWvA4IZv8H/+GQWQEh/kwo95Q9OVW1A==", - "engines": { - "node": ">=14.0.0" - } - }, "node_modules/ts-interface-checker": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz", diff --git a/package.json b/package.json index 0beedece0..e91ae6520 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ "lossless-json": "^2.0.8", "micro-starknet": "^0.2.1", "pako": "^2.0.4", - "ts-custom-error": "^3.3.1", "url-join": "^4.0.1" }, "lint-staged": { diff --git a/src/provider/errors.ts b/src/provider/errors.ts index 7d798d1ca..143671448 100644 --- a/src/provider/errors.ts +++ b/src/provider/errors.ts @@ -1,5 +1,38 @@ +// eslint-disable-next-line max-classes-per-file +export function fixStack(target: Error, fn: Function = target.constructor) { + const { captureStackTrace } = Error as any; + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + captureStackTrace && captureStackTrace(target, fn); +} + +export function fixProto(target: Error, prototype: {}) { + const { setPrototypeOf } = Object as any; + // eslint-disable-next-line @typescript-eslint/no-unused-expressions, no-proto, no-param-reassign + setPrototypeOf ? setPrototypeOf(target, prototype) : ((target as any).__proto__ = prototype); +} + /* eslint-disable max-classes-per-file */ -import { CustomError } from 'ts-custom-error/dist/custom-error'; +export class CustomError extends Error { + name!: string; + + constructor(message?: string) { + super(message); + // set error name as constructor name, make it not enumerable to keep native Error behavior + // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors + // see https://github.com/adriengibrat/ts-custom-error/issues/30 + Object.defineProperty(this, 'name', { + value: new.target.name, + enumerable: false, + configurable: true, + }); + // fix the extended error prototype chain + // because typescript __extends implementation can't + // see https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work + fixProto(this, new.target.prototype); + // try to remove contructor from stack trace + fixStack(this); + } +} export class LibraryError extends CustomError {}