Handle errors in a simple, stable, consistent way.
Hire me
Please
reach out
if you're looking for a Node.js API or CLI engineer (11 years of experience).
Most recently I have been Netlify Build's
and Netlify Plugins'
technical lead for 2.5 years. I am available for full-time remote positions.
try {
throw'Missing file path.'
} catch (invalidError) {
// This fails: `invalidError.message` is `undefined`console.log(invalidError.message.trim())
}
try {
throw'Missing file path.'
} catch (invalidError) {
const normalizedError = BaseError.normalize(invalidError)
// This works: 'Missing file path.'// `normalizedError` is a `BaseError` instance.console.log(normalizedError.message.trim())
}
🐞 Unknown errors
Handling known errors
Known errors should be handled in a try {} catch {} block and
wrapped with a specific class.
That block should only cover the statement that might throw in order to prevent
catching other unrelated errors.
try {
return regExp.test(value)
} catch (error) {
// Now an `InputError` instancethrownewInputError('Invalid regular expression:', { cause: error })
}
Normalizing unknown errors
If an error is not handled as described above, it is
considered unknown. This indicates an unexpected exception, usually a bug.
BaseError.normalize(error, UnknownError)
assigns the UnknownError class to those errors.
The custom option can be used to provide an error class
with additional methods, constructor, properties or options.
exportconstInputError = BaseError.subclass('InputError', {
// The `class` must extend from the parent error classcustom: classextendsBaseError {
// If a `constructor` is defined, its parameters must be (message, options)// Additional `options` can be defined.constructor(message, options) {
message += options?.suffix ?? ''super(message, options)
}
isUserInput() {
// ...
}
},
})
const error = newInputError('Wrong user name', { suffix: ': example' })
console.log(error.message) // 'Wrong user name: example'console.log(error.isUserInput())
We found that modern-errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Last updated on 08 Apr 2024
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.
ESLint has approved an RFC that adds support for TypeScript configuration files, which is aimed at improving the developer experience and recognizing changes in the evolving JavaScript ecosystem.
The NVD is facing a significant backlog with over 12,500 CVEs awaiting analysis, and more than 50% of known exploited vulnerabilities (KEVs) left unenriched since mid-February.
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.