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())
The npm package modern-errors receives a total of 4,238 weekly downloads. As such, modern-errors popularity was classified as popular.
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.
NIST's new AI Risk Management Framework aims to enhance the security and reliability of generative AI systems and address the unique challenges of malicious AI exploits.
This episode of the Risky Biz podcast discusses how the rise of small open source packages and the shift towards individual maintainers makes the ecosystem more vulnerable to supply chain attacks.
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.