From a5d23696e66df6e3f1a43bed7614694ecadea7b8 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 10 Sep 2019 17:44:47 +0200 Subject: [PATCH] Remove ExtError --- CHANGELOG.md | 4 ++ packages/util/src/assert.spec.ts | 10 --- packages/util/src/assert.ts | 13 ++-- packages/util/src/ext/error.spec.ts | 102 ---------------------------- packages/util/src/ext/error.ts | 73 -------------------- packages/util/src/ext/index.ts | 9 --- packages/util/src/index.ts | 1 - packages/util/src/types.ts | 7 -- 8 files changed, 10 insertions(+), 209 deletions(-) delete mode 100644 packages/util/src/ext/error.spec.ts delete mode 100644 packages/util/src/ext/error.ts delete mode 100644 packages/util/src/ext/index.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f43a4d8b5..b8df3bc30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.3.0-beta.x + +- Remove `ExtError`, always prefer the standard JS `Error` object for errors. This would bre a breaking change for any appliactions using `ExtError` + # 1.2.1 - Keyring `addFromAddress` can now be used to store external accounts, for example those provided by external signers diff --git a/packages/util/src/assert.spec.ts b/packages/util/src/assert.spec.ts index e2cb5c42b..18273c2ca 100644 --- a/packages/util/src/assert.spec.ts +++ b/packages/util/src/assert.spec.ts @@ -22,14 +22,4 @@ describe('assert', (): void => { (): boolean => assert(false, (): string => 'message from function') ).toThrow(/message from function/); }); - - it('throws a valid constructed ExtError', (): void => { - try { - assert(false, 'error', -666, 'data'); - } catch (error) { - expect(error.code).toEqual(-666); - expect(error.data).toEqual('data'); - expect(error.message).toEqual('error'); - } - }); }); diff --git a/packages/util/src/assert.ts b/packages/util/src/assert.ts index 5fd69493c..282cbe2d7 100644 --- a/packages/util/src/assert.ts +++ b/packages/util/src/assert.ts @@ -2,7 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import ExtError from './ext/error'; import isFunction from './is/function'; type MessageFn = () => string; @@ -11,9 +10,9 @@ type Falsy = null | undefined | false | 0 | ''; // No NaN type /** * @name assert - * @summary Checks for a valid test, if not ExtError is thrown. + * @summary Checks for a valid test, if not Error is thrown. * @description - * Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned. + * Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an Error with the supplied `message`. When `test` passes, `true` is returned. * @example *
* @@ -21,11 +20,11 @@ type Falsy = null | undefined | false | 0 | ''; // No NaN type * const { assert } from '@polkadot/util'; * * assert(true, 'True should be true'); // true returned - * assert(false, 'False should not be true'); // ExtError thrown - * assert(false, () => 'message'); // ExtError with 'message' + * assert(false, 'False should not be true'); // Error thrown + * assert(false, () => 'message'); // Error with 'message' * ``` */ -export default function assert (test: Falsy | T, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: string | number): test is T { +export default function assert (test: Falsy | T, message: string | MessageFn): test is T { if (test) { return true; } @@ -34,5 +33,5 @@ export default function assert (test: Falsy | T, message: string | MessageFn, message = message(); } - throw new ExtError(message, code, data); + throw new Error(message); } diff --git a/packages/util/src/ext/error.spec.ts b/packages/util/src/ext/error.spec.ts deleted file mode 100644 index fe9cdec8d..000000000 --- a/packages/util/src/ext/error.spec.ts +++ /dev/null @@ -1,102 +0,0 @@ -/* eslint-disable @typescript-eslint/unbound-method */ -// Copyright 2017-2019 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import isError from '../is/error'; -import { ExtError } from '.'; - -describe('ExtError', (): void => { - describe('constructor', (): void => { - it('constructs an Error that is still an Error', (): void => { - expect( - isError( - new ExtError() - ) - ).toEqual(true); - }); - }); - - describe('static', (): void => { - it('exposes the .CODES as a static', (): void => { - expect( - Object.keys(ExtError.CODES) - ).not.toEqual(0); - }); - }); - - describe('constructor properties', (): void => { - it('sets the .message property', (): void => { - expect( - new ExtError('test message').message - ).toEqual('test message'); - }); - - it("sets the .message to '' when not set", (): void => { - expect( - new ExtError().message - ).toEqual(''); - }); - - it('sets the .code property', (): void => { - expect( - new ExtError('test message', 1234).code - ).toEqual(1234); - }); - - it('sets the .code to UKNOWN when not set', (): void => { - expect( - new ExtError('test message').code - ).toEqual(ExtError.CODES.UNKNOWN); - }); - - it('sets the .data property', (): void => { - const data = 'here'; - - expect( - new ExtError('test message', 1234, data).data - ).toEqual(data); - }); - }); - - describe('stack traces', (): void => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let captureStackTrace: (targetObject: Record, constructorOpt?: Function | undefined) => void; - - beforeEach((): void => { - captureStackTrace = Error.captureStackTrace; - - Error.captureStackTrace = function (error): void { - Object.defineProperty(error, 'stack', { - configurable: true, - get: function getStack (): string { - const value = 'some stack returned'; - - Object.defineProperty(this, 'stack', { value }); - - return value; - } - }); - }; - }); - - afterEach((): void => { - Error.captureStackTrace = captureStackTrace; - }); - - it('captures via captureStackTrace when available', (): void => { - expect( - new ExtError().stack - ).toEqual('some stack returned'); - }); - - it('captures via stack when captureStackTrace not available', (): void => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Error.captureStackTrace = null as any; - - expect( - new ExtError().stack.length - ).not.toEqual(0); - }); - }); -}); diff --git a/packages/util/src/ext/error.ts b/packages/util/src/ext/error.ts deleted file mode 100644 index fe2a14f92..000000000 --- a/packages/util/src/ext/error.ts +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2017-2019 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { ExtErrorInterface } from '../types'; - -import isFunction from '../is/function'; - -const UNKNOWN = -99999; - -function extend (that: ExtError, name: string, value?: string | number): void { - Object.defineProperty(that, name, { - configurable: true, - enumerable: false, - value - }); -} - -/** - * @name ExtError - * @summary Extension to the basic JS Error. - * @description - * The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (any types) parameters are available on the object. - * @example - *
- * - * ```javascript - * const { ExtError } from '@polkadot/util'); - * - * throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601 - * ``` - */ -export default class ExtError extends Error implements ExtErrorInterface { - public code: number; - - public data?: number | string; - - public message: string; - - public name: string; - - public stack: string; - - public constructor (message = '', code: number = UNKNOWN, data?: number | string) { - super(); - - // These are actually done via extend below to clean them up - this.code = code; - this.data = data; - this.name = this.constructor.name; - this.message = String(message); - this.stack = ''; - - extend(this, 'message', String(message)); - extend(this, 'name', this.constructor.name); - extend(this, 'data', data); - extend(this, 'code', code); - - // eslint-disable-next-line @typescript-eslint/unbound-method - if (isFunction(Error.captureStackTrace)) { - Error.captureStackTrace(this, this.constructor); - } else { - extend(this, 'stack', (new Error(message)).stack); - } - } - - public static CODES = { - ASSERT: -90009, - UNKNOWN, - INVALID_JSONRPC: -99998, - METHOD_NOT_FOUND: -32601 // Rust client - }; -} diff --git a/packages/util/src/ext/index.ts b/packages/util/src/ext/index.ts deleted file mode 100644 index f73ae68c8..000000000 --- a/packages/util/src/ext/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2017-2019 @polkadot/util authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -/** - * @summary Extensions to basic classes - */ - -export { default as ExtError } from './error'; diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 3bb8f1112..964c24695 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -17,7 +17,6 @@ export * from './array'; export * from './bn'; export * from './buffer'; export * from './compact'; -export * from './ext'; export * from './format'; export * from './hex'; export * from './is'; diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index a0861966f..df093665e 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -8,13 +8,6 @@ export interface ToBn { toBn: () => BN; } -export interface ExtErrorInterface { - code: number; - data?: string | number; - message: string; - stack: string; -} - // eslint-disable-next-line @typescript-eslint/no-explicit-any type Logger$Data$Fn = () => any[]; // eslint-disable-next-line @typescript-eslint/no-explicit-any