-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbeb138
commit 261864d
Showing
5 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/handlersjs-logging/lib/util/make-error-loggable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { makeErrorLoggable } from './make-error-loggable'; | ||
|
||
describe('makeErrorLoggable', () => { | ||
|
||
it('should return a set values when passed something that is not an Error', async () => { | ||
|
||
expect(makeErrorLoggable('foo')).toEqual({ 'error': 'not-an-error' }); | ||
|
||
}); | ||
|
||
it('should return a parsed object when passed an Error', async () => { | ||
|
||
const error = new Error('foo'); | ||
|
||
expect(makeErrorLoggable(error)).toEqual({ | ||
message: 'foo', | ||
name: 'Error', | ||
stack: expect.any(Array), | ||
}); | ||
|
||
}); | ||
|
||
it('should return an empty stack when passed an Error without a stack', async () => { | ||
|
||
const error = new Error('foo'); | ||
delete error.stack; | ||
|
||
expect(makeErrorLoggable(error)).toEqual({ | ||
message: 'foo', | ||
name: 'Error', | ||
stack: [], | ||
}); | ||
|
||
}); | ||
|
||
}); |
15 changes: 15 additions & 0 deletions
15
packages/handlersjs-logging/lib/util/make-error-loggable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const makeErrorLoggable = (error: unknown): Record<string, unknown> => { | ||
|
||
if (error instanceof Error) { | ||
|
||
return { | ||
message: error.message, | ||
name: error.name, | ||
stack: error.stack ? error.stack.split(/\n/) : [], | ||
}; | ||
|
||
} | ||
|
||
return { 'error': 'not-an-error' }; | ||
|
||
}; |