The stack trace is neither
verbose nor redundant,
while still keeping all information
Wrap error message
The outer error message is appended.
try {
awaitreadFile(filePath)
} catch (cause) {
thrownewInputError(`Could not read ${filePath}`, { cause })
// InputError: File does not exist.// Could not read /example/path
}
If the outer error message ends with :, it is prepended instead.
thrownewInputError(`Could not read ${filePath}:`, { cause })
// InputError: Could not read /example/path: File does not exist.
: can optionally be followed a newline.
thrownewInputError(`Could not read ${filePath}:\n`, { cause })
// InputError: Could not read /example/path:// File does not exist.
Error types
Test error type
Once errorHandler() has been applied, the error type can be
checked by its name. Libraries should document their possible error names, but
do not need to export their error types.
try {
thrownewAuthError('Could not authenticate.')
} catch (cause) {
thrownewInputError('Could not read the file.', { cause })
// Now an InputError
}
However, the inner error type is kept if the outer one is Error or
AggregateError.
try {
thrownewAuthError('Could not authenticate.')
} catch (cause) {
thrownewError('Could not read the file.', { cause })
// Still an AuthError
}
Unknown errors
All errors should use known types: the ones returned by
modernErrors(). Errors with an unknown type
should be handled in try {} catch {} and re-thrown with a
known type instead.
The errorHandler() assigns the UnknownError type to any
error with an unknown type.
const getUserId = function (user) {
return user.id
}
getUserId(null) // UnknownError: Cannot read properties of null (reading 'id')
The error must be from a known type. However, any other
error (including Error, TypeError, RangeError, etc.) is also serializable
providing it has been either passed to errorHandler(), or
wrapped as an error.cause.
parse(errorObject) converts those error plain objects back to
identical error instances.
The original error type is generically preserved. However, it is converted to a
generic Error if it is neither a native type (TypeError, RangeError, etc.)
nor a known type.
const newErrorObject = JSON.parse(errorString)
const newError = parse(newErrorObject)
// InputError: Could not read the file.// filePath: '/path'// [cause]: Error: ...
Deep serialization/parsing
Objects and arrays containing custom errors can be deeply serialized to JSON.
They can then be deeply parsed back using
JSON.parse()'s reviver.
const error = newInputError('Could not read the file.')
const deepObject = [{}, { error }]
const jsonString = JSON.stringify(deepObject)
const newDeepObject = JSON.parse(jsonString, (key, value) =>parse(value))
console.log(newDeepObject[1].error) // InputError: Could not read the file.
Modules
This framework brings together a collection of modules which can also be used
individually:
The npm package modern-errors receives a total of 7,004 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.
Package last updated on 21 Aug 2022
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.
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.