Add Blake2s & XXHash utility functions (#66)
* Initial split of util & util-crypto * Add initial XXHash version * Blake2s wit tests & documentation * Add sha512 (from NaCl) * Add randomBytes (from NaCl) * Flow fixes * Update documentation * Update latest @polkadot/dev
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ExtErrorInterface } from '../types';
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const UNKNOWN = -99999;
|
||||
|
||||
/**
|
||||
@name ExtError
|
||||
@signature ExtError (message: string = '', code: number = UNKNOWN_ERROR, value: any)
|
||||
@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` (mixed types) parameters are available on the object.
|
||||
@example
|
||||
const { ExtError } = require('@polkadot/util');
|
||||
|
||||
throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
|
||||
*/
|
||||
module.exports = class ExtError extends Error implements ExtErrorInterface {
|
||||
code: number;
|
||||
data: mixed;
|
||||
message: string;
|
||||
name: string;
|
||||
stack: string;
|
||||
|
||||
constructor (message: string = '', code: number = UNKNOWN, data: mixed) {
|
||||
super();
|
||||
|
||||
const extend = (name: string, value: mixed): void => {
|
||||
Object.defineProperty(this, name, {
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
value
|
||||
});
|
||||
};
|
||||
|
||||
extend('message', String(message));
|
||||
extend('name', this.constructor.name);
|
||||
extend('data', data);
|
||||
extend('code', code);
|
||||
|
||||
if (isFunction(Error.captureStackTrace)) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
} else {
|
||||
extend('stack', (new Error(message)).stack);
|
||||
}
|
||||
}
|
||||
|
||||
static CODES = {
|
||||
ASSERT: -90009,
|
||||
UNKNOWN,
|
||||
INVALID_JSONRPC: -99998,
|
||||
METHOD_NOT_FOUND: -32601 // Rust client
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isError = require('../is/error');
|
||||
const { ExtError } = require('./index');
|
||||
|
||||
describe('ExtError', () => {
|
||||
describe('constructor', () => {
|
||||
it('constructs an Error that is still an Error', () => {
|
||||
expect(
|
||||
isError(
|
||||
new ExtError()
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('static', () => {
|
||||
it('exposes the .CODES as a static', () => {
|
||||
expect(
|
||||
Object.keys(ExtError.CODES)
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('constructor properties', () => {
|
||||
it('sets the .message property', () => {
|
||||
expect(
|
||||
new ExtError('test message').message
|
||||
).toEqual('test message');
|
||||
});
|
||||
|
||||
it("sets the .message to '' when not set", () => {
|
||||
expect(
|
||||
new ExtError().message
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('sets the .code property', () => {
|
||||
expect(
|
||||
new ExtError('test message', 1234).code
|
||||
).toEqual(1234);
|
||||
});
|
||||
|
||||
it('sets the .code to UKNOWN when not set', () => {
|
||||
expect(
|
||||
new ExtError('test message').code
|
||||
).toEqual(ExtError.CODES.UNKNOWN);
|
||||
});
|
||||
|
||||
it('sets the .data property', () => {
|
||||
const data = { some: { value: 'here' } };
|
||||
|
||||
expect(
|
||||
new ExtError('test message', 1234, data).data
|
||||
).toEqual(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stack traces', () => {
|
||||
let captureStackTrace;
|
||||
|
||||
beforeEach(() => {
|
||||
captureStackTrace = Error.captureStackTrace;
|
||||
|
||||
Error.captureStackTrace = function (error) {
|
||||
Object.defineProperty(error, 'stack', {
|
||||
configurable: true,
|
||||
get: function getStack () {
|
||||
const value = 'some stack returned';
|
||||
|
||||
Object.defineProperty(this, 'stack', { value });
|
||||
|
||||
return value;
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Error.captureStackTrace = captureStackTrace;
|
||||
});
|
||||
|
||||
it('captures via captureStackTrace when available', () => {
|
||||
expect(
|
||||
new ExtError().stack
|
||||
).toEqual('some stack returned');
|
||||
});
|
||||
|
||||
it('captures via stack when captureStackTrace not available', () => {
|
||||
Error.captureStackTrace = null;
|
||||
|
||||
expect(
|
||||
new ExtError().stack.length
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const ExtError = require('./error');
|
||||
|
||||
/**
|
||||
@summary Extensions to basic classes
|
||||
*/
|
||||
module.exports = {
|
||||
ExtError
|
||||
};
|
||||
Reference in New Issue
Block a user