Skip to content

Commit

Permalink
Support node.js inspect natively
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Apr 1, 2018
1 parent bc4280a commit 7167b39
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Compatible with node.js and browsers
* Works with `instanceof`
* Output full stack trace with `fullStack(err)`
* Automatic full stack traces with node.js (via [`inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options))
* Extends [`make-error`](https://github.com/JsCommunity/make-error)

## Installation
Expand All @@ -36,6 +37,8 @@ const customError = new CustomError('Another boom!', error)

console.log(fullStack(error)) // Works with any error.
console.log(fullStack(customError)) // Extended stack trace contains error causes.

console.log(customError instanceof Error) //=> true
```

## Attribution
Expand Down
3 changes: 3 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test = require('blue-tape')
import { inspect } from 'util'
import { BaseError, fullStack } from './index'

const SEP_TEXT = '\n\nDuring the above error, another error occurred:\n\n'
Expand All @@ -17,12 +18,14 @@ test('make error cause', t => {

t.equal(testError.cause, cause)
t.equal(fullStack(testError), `${cause.stack}${SEP_TEXT}${testError.stack}`)
t.equal(inspect(testError), fullStack(testError))
t.ok(testError instanceof Error)
t.ok(testError instanceof BaseError)
t.ok(testError instanceof TestError)

t.equal(subTestError.cause, testError)
t.equal(fullStack(subTestError), `${cause.stack}${SEP_TEXT}${testError.stack}${SEP_TEXT}${subTestError.stack}`)
t.equal(inspect(subTestError), fullStack(subTestError))
t.ok(subTestError instanceof Error)
t.ok(subTestError instanceof BaseError)
t.ok(subTestError instanceof TestError)
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export class BaseError extends makeError.BaseError {
super(message)
}

inspect () {
return fullStack(this)
}

}

export function fullStack (error: Error | BaseError) {
Expand Down

0 comments on commit 7167b39

Please sign in to comment.