
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
catch-decorator
Advanced tools
Allows you to handle exceptions in class methods with only one annotation (decorator). Idea of errors handling taken from Java.
UPDATE from v2: refactored to use stacked decorators style. Thx @k1r0s for idea :)
npm install catch-decorator
The main problem of handling errors are using "try/catch" blocks or "catch" methods for Promises. But if we use classes, for example for Vue components, why can't we use method decorators for handling errors?
So, for example, instead of this:
class Messenger {
async getMessages() {
try
await api.getData() // <-- can throw ServerError
} catch(err) {
...
}
}
}
we can write this:
import Catch from 'catch-decorator'
class Messenger {
@Catch(ServerError, handler)
async getMessages() {
await api.getData() // <-- can throw custom ServerError
}
}
much prettier, isn't it?
catch-decorator
works with any ECMAScript/Typescript classes. If you use Babel, babel-plugin-transform-decorators-legacy is needed. If you use TypeScript, enable--experimentalDecorators
flag.
You can handle any thrown error:
import Catch from 'catch-decorator'
const CatchAll = Catch(Error, (err: any) => console.log(err.message))
class Messenger {
@CatchAll
getMessages() {
throw new TypeError('ReferenceError here!')
...
}
}
or write decorators in stack to handle more than one errors type. In callback as second argument will be passed current instance object (context):
class Messenger {
@Catch(TypeError, (err, ctx) => {...})
@Catch(ReferenceError, (err, ctx) => {...})
getMessages() {
throw new ReferenceError('ReferenceError here!')
...
}
}
It also works with async methods:
class Messenger {
errorMessage = null
@Catch(ServerError, (err, ctx) => ctx.errorMessage = err.message)
getMessages() {
return fetch(myRequest).then(response => { // can throw ServerError
...
})
}
}
MIT
FAQs
Let you handle exceptions with just one annotation
The npm package catch-decorator receives a total of 4,393 weekly downloads. As such, catch-decorator popularity was classified as popular.
We found that catch-decorator 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
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.