This repository has been archived on 2026-07-15. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
drive/frontend/node_modules/custom-error-instance/examples/map.js
Elijah 724d70e58b
Some checks failed
Automated Container Build / build-and-push (push) Failing after 12s
Inital build
2026-05-22 12:29:43 -07:00

25 lines
No EOL
720 B
JavaScript

"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;
}
}