This commit is contained in:
parent
fa2be029a2
commit
724d70e58b
3339 changed files with 1075535 additions and 0 deletions
46
frontend/node_modules/custom-error-instance/examples/about.js
generated
vendored
Normal file
46
frontend/node_modules/custom-error-instance/examples/about.js
generated
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
var CustomError = require('../index');
|
||||
|
||||
// define a custom error with a default message
|
||||
var Parent = CustomError('ParentError', { message: 'Parent error' });
|
||||
|
||||
// define a custom error that inherits from the Parent custom error
|
||||
var Child = CustomError('ChildError', Parent, { message: 'Child error' });
|
||||
|
||||
var e;
|
||||
|
||||
// create an error instance that uses defaults
|
||||
e = Parent();
|
||||
console.log(e.toString()); // "ParentError: Parent error"
|
||||
console.log(e.message); // "Parent error"
|
||||
console.log(e.name); // "ParentError"
|
||||
console.log(e.constructor.name); // "ParentError"
|
||||
console.log(e instanceof Parent); // true
|
||||
console.log(e instanceof Error); // true
|
||||
|
||||
// create an error instance that overwrites the default message
|
||||
e = Parent('Hello');
|
||||
console.log(e.toString()); // "ParentError: Hello"
|
||||
console.log(e.message); // "Hello"
|
||||
console.log(e.name); // "ParentError"
|
||||
console.log(e.constructor.name); // "ParentError"
|
||||
console.log(e instanceof Parent); // true
|
||||
console.log(e instanceof Error); // true
|
||||
|
||||
// create an error instance that overwrites the default message and defines a code
|
||||
e = Parent({ message: 'Hello', code: 'XYZ' });
|
||||
console.log(e.toString()); // "ParentError XYZ: Hello"
|
||||
console.log(e.message); // "Hello"
|
||||
console.log(e.name); // "ParentError"
|
||||
console.log(e.constructor.name); // "ParentError"
|
||||
console.log(e instanceof Parent); // true
|
||||
console.log(e instanceof Error); // true
|
||||
|
||||
// create an error instance of the Child custom error
|
||||
e = Child();
|
||||
console.log(e.toString()); // "ParentError: Child error"
|
||||
console.log(e.message); // "Child error"
|
||||
console.log(e.name); // "ChildError"
|
||||
console.log(e.constructor.name); // "ChildError"
|
||||
console.log(e instanceof Child); // true
|
||||
console.log(e instanceof Parent); // true
|
||||
console.log(e instanceof Error); // true
|
||||
25
frontend/node_modules/custom-error-instance/examples/map.js
generated
vendored
Normal file
25
frontend/node_modules/custom-error-instance/examples/map.js
generated
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
"use strict";
|
||||
var CustomError = require('../index');
|
||||
var store = {};
|
||||
|
||||
var Err = CustomError('MapError');
|
||||
Err.inuse = CustomError(Err, { message: 'The specified key is already in use.', code: 'INUSE' });
|
||||
|
||||
function add(key, value) {
|
||||
if (Math.random() < .3) throw new Err('Random Error');
|
||||
if (store.hasOwnProperty(key)) throw new Err.inuse();
|
||||
store[key] = value;
|
||||
}
|
||||
|
||||
try {
|
||||
add('x', 1);
|
||||
add('x', 2);
|
||||
} catch (e) {
|
||||
if (e instanceof Err.inuse) {
|
||||
console.error(e.toString()); // "MapError INUSE: The specified key is already in use."
|
||||
} else if (e instanceof Err) {
|
||||
console.error('Unexpected ' + e); // "Unexpected MapError: Random Error"
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
Reference in a new issue