Skip to content

An error that takes additional properties in the constructor

License

Notifications You must be signed in to change notification settings

ukitgroup/eerror-js

Repository files navigation

EError

An error that can easily combine with additional data

Commitizen friendlyBuild StatusCoverage Status

Greenkeeper badgedependencies StatusdevDependencies Status

npmnodeMIT License

NPM

Usecase

In clear js we do that:

functionerrored(){/// ...consterror=newError('Entity not found');error.status=404;error.code='NOT_FOUND';error.query={/* ... */};throwerror;}

With this library we can do that:

importEErrorfrom'eerror';functionerrored(){/// ...thrownewEError('Entity not found').combine({ query,code: 'NOT_FOUND',status: 404,});}// orfunctionerrored(){/// ...thrownewEError().combine({message: 'Entity not found',code: 'NOT_FOUND',status: 404, query,});}

For clear logs don't combine error with very huge object, like models from database. Just put the most important data, such as the entity id or error codes from external services.

Error wrapping

Use static method wrap to add data to existing errors:

asyncfunctiontryAndCatch(user){try{awaituser.foo();}catch(error){// this call only apply params to error objectthrowEError.wrap(error,{userId: user.id});}}

Also, EError object has method wrap, who assign all of properties of EError instance to error, but not name and stack.

asyncfunctiontryAndCatch(user){try{awaituser.foo();}catch(error){// this call apply all of properties from EError instance to error objectthrownewEError().combine({ query }).wrap(error,{userId: user.id});// thrower error with 'query' property, userId and all of data from error object// stacktrace and name properties will be get from error object}}

Prepared errors

You can create prepared error constructors:

constMyBuisnessError=EError.prepare({message: 'Something wen wrong',code: 42});MyBuisnessErrorinstanceofEError// trueasyncfunctionerrored(){if(moonPhase){thrownewMyBuisnessError({ moonPhase });// error will contain correct stacktrace,// code = 42 and moonPhase value,// message will be equal 'Something wen wrong'}}

You can create prepared error extends from custom class:

// Base class must be extends from EError or childsconstMyBuisnessError2=EError.prepare(MyBuisnessError,{success: false});MyBuisnessErrorinstanceofEError// trueMyBuisnessError2instanceofEError// trueasyncfunctionerrored(){if(moonPhase){thrownewMyBuisnessError({ moonPhase });// error will contain correct stacktrace,// code = 42, moonPhase value, success = false// and message will be equal 'Something wen wrong'}}

Also you can wrap errors from prepared error:

constMyBuisnessError=EError.prepare({message: 'Something wen wrong',code: 42});asyncfunctiontryAndCatch(user){try{awaituser.foo();}catch(error){// throwed error message will be 'Something wen wrong',// and contain code = 42 and userId// stack and other additional properties from error will be savedthrowMyBuisnessError.wrap(error,{userId: user.id});}}

Conflicts resolving

What about conflicts resolving? How don't loose data when we combine error who contain 'code' with options{code: ... }? Yes, save the data in the renamed property!

consterror=newEError().combine({message: 'first'}).combine({message: 'second'});.combine({message: 'N + 1'});error.message==='N + 1';error.message_old2==='second';error.message_old1==='first';

Because it error, you don't interact with this dynamic named properties manual, but you will se replaced data in logs. If you want, you can filtred 'backup' properties in your logger layer by regexp /.+_old\d+/

About

An error that takes additional properties in the constructor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%