error-format
v1.0.1
Published
Allows you to customize the toString method of passed `err`. Also adds useful properties like `line`, `filename` and `column` to the `err` object.
Downloads
14
Readme
error-format
Allows you to customize the toString method of passed
err
. Also adds useful properties likeline
,filename
andcolumn
to theerr
object.
Install
npm i error-format --save
Usage
For more use-cases see the tests
const errorFormat = require('error-format')
errorFormat
Adds bypassed
.toString
which you can customize through thefmt
function.
Params
<err>
{Error}: error object/instance[fmt]
{Function}: custom format functionreturns
{Error}: what comes from input (instance of error)
Example
var errorFormat = require('error-format')
var err = new TypeError('baz qux')
console.log(err.toString())
// => TypeError: baz qux
err = errorFormat(err, function fmt (headline) {
if (this.message.indexOf('baz') !== -1) {
headline += ' --- Line: ' + this.line
}
if (this.message.indexOf('qux') !== -1) {
headline += ' --- Column: ' + this.column
}
return headline
})
console.log(err.toString())
// => TypeError: baz qux --- Line: 4 --- Column: 11
Related
- error-base: Create custom Error classes. | homepage
- is-typeof-error: Check that given value is any type of error and… more | homepage
- kind-error: Base class for easily creating meaningful and quiet by default… more | homepage
- kind-of: Get the native type of a value. | homepage
- kind-of-extra: Additional functionality to
kind-of
type check utility, support promises, generators,… more | homepage - kind-of-types: List of all javascript types. Used and useful for checking,… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.