+1
-1
@@ -26,7 +26,7 @@
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-babel && polkadot-dev-build-docs",
|
||||
"check": "eslint src && flow check",
|
||||
"test": "jest src"
|
||||
"test": "jest --coverage src"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/api-jsonrpc": "^0.5.5",
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const ExtError = require('./ext/error');
|
||||
|
||||
/**
|
||||
@name assert
|
||||
@signature assert (test: boolean, message: string, code: number = ExtError.CODES.ASSERT, data: any): void
|
||||
@summary Checks for a valid test, if not ExtError is thrown.
|
||||
@description
|
||||
Checks that `test` is a true value. If value is `false` (or `false-ish`), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned.
|
||||
@example
|
||||
const assert = require('@polkadot/util/assert');
|
||||
|
||||
assert(true === true, 'True should be true'); // true returned
|
||||
assert(false === true, 'False should not be true'); // ExtError thrown
|
||||
*/
|
||||
module.exports = function assert (test: boolean, message: string, code: number = ExtError.CODES.ASSERT, data: any): boolean {
|
||||
if (!test) {
|
||||
throw new ExtError(message, code, data);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { assert } = require('./index');
|
||||
|
||||
describe('assert', () => {
|
||||
it('should not throw an error when test is true', () => {
|
||||
expect(
|
||||
assert(true, 'nothing should be thrown')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('should throw an error when test is not true', () => {
|
||||
expect(
|
||||
() => assert(false, 'error thrown')
|
||||
).toThrow(/error thrown/);
|
||||
});
|
||||
|
||||
it('throws a valid constructed ExtError', () => {
|
||||
try {
|
||||
assert(false, 'error', -666, { some: 'data' });
|
||||
} catch (error) {
|
||||
expect(error.code).toEqual(-666);
|
||||
expect(error.data).toEqual({ some: 'data' });
|
||||
expect(error.message).toEqual('error');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -49,6 +49,7 @@ module.exports = class ExtError extends Error implements ExtErrorInterface {
|
||||
}
|
||||
|
||||
static CODES = {
|
||||
ASSERT: -90009,
|
||||
UNKNOWN,
|
||||
INVALID_JSONRPC: -99998,
|
||||
METHOD_NOT_FOUND: -32601 // Rust client
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const assert = require('./assert');
|
||||
const bn = require('./bn');
|
||||
const buffer = require('./buffer');
|
||||
const ext = require('./ext');
|
||||
@@ -16,5 +17,5 @@ const u8a = require('./u8a');
|
||||
@summary Utility methods for this package are split into groups
|
||||
*/
|
||||
module.exports = Object.assign(
|
||||
{}, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a
|
||||
{}, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user