Remove ExtError

This commit is contained in:
Jaco Greeff
2019-09-10 17:44:47 +02:00
parent 5adc753f81
commit a5d23696e6
8 changed files with 10 additions and 209 deletions
+4
View File
@@ -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
-10
View File
@@ -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');
}
});
});
+6 -7
View File
@@ -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
* <BR>
*
@@ -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<T> (test: Falsy | T, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: string | number): test is T {
export default function assert<T> (test: Falsy | T, message: string | MessageFn): test is T {
if (test) {
return true;
}
@@ -34,5 +33,5 @@ export default function assert<T> (test: Falsy | T, message: string | MessageFn,
message = message();
}
throw new ExtError(message, code, data);
throw new Error(message);
}
-102
View File
@@ -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<string, any>, 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);
});
});
});
-73
View File
@@ -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
* <BR>
*
* ```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
};
}
-9
View File
@@ -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';
-1
View File
@@ -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';
-7
View File
@@ -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