Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
browser-error-classes
Advanced tools
## Errors : <code>object</code> # Custom and Explicit Javascript Error Classes
object
bower install browser-error-classes
Easy to read custom and extensible error classes for the browser which extend the native error classes.
Include all the errors with Error.js
OR include just what you need from the lib/
dir.
Sometimes, just throwing an error isn't that helpful. Sometimes, handling an error elegantly preventing a crash just isn't quite good enough sometimes you want to handle explicit errors in the UI in specific ways. During team development there are times when you want to throw explicit errors to both help developers understand whats going on, as well as enforce some strict rules. That is what this module is for.
Kind: global namespace
Example
<script src='bower_components/browser-error-classes/Errors.js'></script>
object
Error
Error
Error
Error
Error
TypeError
Error
Error
Kind: static class of Errors
Extends: Error
Error
Error for Immutable variables
String
Kind: static method of Immutable
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
varaibleName | String | name of immutable variable |
scope | Any | Scope of varaible |
Example
class User{
constructor(name,age){
Object.defineProperties(
this,
{
age:{
enumerable:true,
writable:true,
value:age
},
name:{
enumerable:true,
get:getName,
set:setName
}
}
);
let userName=null;
function getName(){
return userName;
}
function setName(value){
if(userName){
const err=new Errors.Immutable;
err.setMessage(
'name',
this
);
throw err;
}
userName=value;
return userName;
}
if(name){
this.name=name;
}
}
}
const bob=new User('bob',42);
bob.name='bob';
Example
immutable.html:39 Uncaught Immutable: "name" is Immutable and may not be modified.
Scope : {
"age": 42,
"name": "bob"
}
Error
Kind: static class of Errors
Extends: Error
Error for methods which are either undefined or not methods (functions)
String
Kind: static method of InvalidMethod
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
methodName | String | method name |
method | Any | expected method |
[scope] | Any | scope in which the invalid method is expected |
Example
class User{
constructor(name,age){
this.name=name;
this.age=age;
}
}
const bob=new User('bob',42);
if(!bob.getInfo || typeof bob.getInfo !== 'function'){
const err=new Errors.InvalidMethod;
err.setMessage('getInfo',bob.getInfo,bob);
}
Example
invalidMethod.html:25 Uncaught InvalidMethod: Expects "getInfo" to be Function but got undefined
Scope : {
"name": "bob",
"age": 42
}
Error
Kind: static class of Errors
Extends: Error
Error
Error for invalid parameters
String
Kind: static method of InvalidParameter
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
parameterName | Any | name of parameter |
expected | Any | what it expected |
got | Any | what it got |
[scope] | Any | optional value where the parameter came from like an object or array |
Example
let test={a:1,b:0};
if(test.b<1){
err=new Errors.InvalidParameter;
err.setMessage(
'b',
'a value greater than 0',
test.b,
test
);
throw err;
}
Example
InvalidParameter: 'b' Expects 'a value greater than 0' but got 0
at InvalidParameter (/home/bmiller/git/node-error-classes/lib/InvalidParameter.js:11:1)
at multiplyNumbers (/home/bmiller/git/node-error-classes/example/invalidParam.js:13:13)
Error
Kind: static class of Errors
Extends: Error
error for required params that are not set or passed
String
Kind: static method of RequiredParameter
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
parameterName | Any | name of parameter |
[scope] | Any | optional value where the parameter came from like an object or array |
Example
let test={a:1,b:0};
if(!test.c){
err=new Errors.RequiredParameter;
err.setMessage(
'c',
test
);
throw err;
}
Example
RequiredParameter: Expects 'numberOne' to be defined
at RequiredParameter (/home/bmiller/git/node-error-classes/lib/RequiredParameter.js:12:1)
at multiplyNumbers (/home/bmiller/git/node-error-classes/example/requiredParam.js:13:13)
Error
Kind: static class of Errors
Extends: Error
Error for when an expected socket is not available
String
Kind: static method of SocketUnavailable
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
socketPath | Any | name of parameter |
[scope] | Any | optional value with information on the socket or constructor |
Example
const ipc=require('node-ipc');
const Errors=require('node-error-classes');
ipc.config.id = 'hello';
ipc.config.maxRetries = 3;
ipc.config.silent=true;
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'destroy',
function(data){
const err=new Errors.SocketUnavailable;
err.setMessage(
ipc.of.world.path,
ipc.of.world
);
throw err;
}
);
}
);
Example
SocketUnavailable: Socket of '/tmp/app.world' Unavailable
at SocketUnavailable (/home/bmiller/git/node-error-classes/lib/SocketUnavailable.js:11:1)
at Object.<anonymous> (/home/bmiller/git/node-error-classes/example/socketUnavailable.js:14:27)
at Object.pub (/home/bmiller/git/node-error-classes/node_modules/node-ipc/node_modules/event-pubsub/event-pubsub.js:69:19)
at Object.trigger (/home/bmiller/git/node-error-classes/node_modules/node-ipc/node_modules/event-pubsub/event-pubsub.js:111:21)
at Socket.connectionClosed (/home/bmiller/git/node-error-classes/node_modules/node-ipc/dao/client.js:157:24)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Pipe._onclose (net.js:469:12)
TypeError
Kind: static class of Errors
Extends: TypeError
TypeError
Used for normalizing the message of a type error
String
Kind: static method of Type
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
parameterName | Any | name of parameter |
type | String | Type String |
value | Any | value that caused error |
[scope] | Any | optional value where the parameter came from like an object or array |
Example
let test={a:1,b:0};
if(typeof test.b!=='object'){
err=new Errors.Type;
err.setMessage(
'b',
'Object',
test.b,
test
);
throw err;
}
Example
git/node-error-classes/example/typeError.js:19
throw err;
^
TypeError: 'numberOne' Expects String but got number : 6
at Type (/home/bmiller/git/node-error-classes/lib/Type.js:12:1)
at multiplyNumbers (/home/bmiller/git/node-error-classes/example/typeError.js:13:13)
Error
Kind: static class of Errors
Extends: Error
Error for undefined values
String
Kind: static method of UndefinedValue
Returns: String
- compiled error message
Param | Type | Description |
---|---|---|
variableName | String | varible name |
variable | Any | varible |
[scope] | Any | scope in which the variable should exist |
Example
const user={
name:'bob'
}
if(!user.age){
const err = new Errors.UndefinedValue;
err.setMessage(
'age',
user.age,
user
);
throw err;
}
Example
git/node-error-classes/example/undefinedValue.js:14
throw err;
^
Undefined: 'string'
FAQs
## Errors : <code>object</code> # Custom and Explicit Javascript Error Classes
We found that browser-error-classes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.