0.4.2 • Published 7 years ago
promise-catcher v0.4.2
Promise Catcher
Simple promise try catch wrapper written in Typescript. Suggestions are welcome.
Installation
You can grab the latest version via NPM.
npm install --save promise-catcher
Then use require through Typescript.
import * as catcher from 'promise-catcher'
The Problem
When using ES6 promises with packages that do not catch your async rejections you may be familiar with this error.
(node:) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id:): RequestError:
To solve this we need to wrap our calls in try catch statements to make sure we are handling them correctly.
//Using callbacks in express
app.get('/url', () => {
return async (req, res, next) => {
try{
if (problem){
throw new Error('message')
}
}catch(err){
next(err)
return
}
next()
}
}())
//Using throws in jasmine
describe('Test', () => {
it('test', () => {
return async done => {
try{
if (problem){
throw new Error('message')
}
}catch(err){
throw err
}
done()
}
}())
})
Our Solution
This package contains convenience functions that wrap your async functions in try catch and pass the error onwards properly.
import * as catcher from 'promise-catcher'
//Using callbacks in express
app.get('/url', catcher.express(async (req, res, next) => {
if (problem){
throw new Error('message')
}
next()
}))
//Using throws in jasmine
describe('Test', catcher.jasmine(async done => {
if (problem){
throw new Error('message')
}
done()
}))
//Use with any callbacks
catcher.wraps(...)
//Use with any throwing
catcher.throws(...)
0.4.2
7 years ago
0.4.0
7 years ago
0.3.8
7 years ago
0.3.7
7 years ago
0.3.6
7 years ago
0.3.5
7 years ago
0.3.4
7 years ago
0.3.3
7 years ago
0.3.2
7 years ago
0.3.1
7 years ago
0.2.10
7 years ago
0.1.10
7 years ago
0.1.9
7 years ago
0.1.8
7 years ago
0.1.7
7 years ago
0.1.6
7 years ago
0.1.5
7 years ago
0.1.4
7 years ago
0.1.3
7 years ago
0.1.2
7 years ago
0.1.1
7 years ago
0.1.0
7 years ago