Skip to content

Commit

Permalink
feat: serialize error cause and help properties
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 7, 2023
1 parent 0c43729 commit 1254c6c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ declare module "youch" {
error: {
message: string;
name: string;
cause?: any;
help?: any;
status: number;
frames: {
file: string;
Expand Down
2 changes: 2 additions & 0 deletions src/Youch.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ class Youch {
callback = callback || this._serializeFrame.bind(this)
return {
message: this.error.message,
help: this.error.help,
cause: this.error.cause,
name: this.error.name,
status: this.error.status,
frames:
Expand Down
28 changes: 28 additions & 0 deletions test/youch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,32 @@ test.group('Youch', () => {
})
.catch(done)
})

test('serialize error cause', (assert, done) => {
const cause = new Error('The main error')
const error = new Error('foo', { cause })
const youch = new Youch(error, {})
youch
.toJSON()
.then(({ error }) => {
assert.equal(error.frames[0].filePath, __filename.replace(/\\/g, '/'))
assert.equal(error.frames[0].isNative, false)
assert.strictEqual(error.cause, cause)
done()
}).catch(done)
})

test('serialize error help', (assert, done) => {
const error = new Error('foo')
error.help = ['This is the error help text']
const youch = new Youch(error, {})
youch
.toJSON()
.then(({ error }) => {
assert.equal(error.frames[0].filePath, __filename.replace(/\\/g, '/'))
assert.equal(error.frames[0].isNative, false)
assert.deepEqual(error.help, ['This is the error help text'])
done()
}).catch(done)
})
})
28 changes: 28 additions & 0 deletions test/youch.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,32 @@ test.group('Youch | ESM', () => {
})
.catch(done)
})

test('serialize error cause', (assert, done) => {
const cause = new Error('The main error')
const error = new Error('foo', { cause })
const youch = new Youch(error, {})
youch
.toJSON()
.then(({ error }) => {
assert.equal(error.frames[0].filePath, __filename.replace(/\\/g, '/'))
assert.equal(error.frames[0].isNative, false)
assert.strictEqual(error.cause, cause)
done()
}).catch(done)
})

test('serialize error help', (assert, done) => {
const error = new Error('foo')
error.help = ['This is the error help text']
const youch = new Youch(error, {})
youch
.toJSON()
.then(({ error }) => {
assert.equal(error.frames[0].filePath, __filename.replace(/\\/g, '/'))
assert.equal(error.frames[0].isNative, false)
assert.deepEqual(error.help, ['This is the error help text'])
done()
}).catch(done)
})
})

0 comments on commit 1254c6c

Please sign in to comment.