-
Notifications
You must be signed in to change notification settings - Fork 6
feat(server): introduces serializeError and redactError #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
160dd6b
feat: breakout error handling into seperate file
reggi ac30cf4
fix: enable circulairty and retain errorType
reggi 7ba8ad4
comments
reggi 1466465
logs strings when serialized, test safe throw
reggi ab525e2
adds server error test to ensure you cant export _redact
reggi 15907d4
added status in addition to statusCode
reggi eb449f1
remove ? in constructor?
reggi 8b32ef7
adopts redaction within error and it's properties, shifting code around
reggi 57852d5
adds some more tests to clarify
reggi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| /** takes an error object and serializes it to a plan object */ | ||
| function serializedError (input) { | ||
| if (!(input instanceof Error)) { | ||
| const error = new Error('attempted to serialize a non-error object') | ||
| return serializedError(error) | ||
| } | ||
reggi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| errorType: input?.constructor?.name, | ||
reggi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
reggi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ...(input.message ? { message: input.message } : {}), | ||
| ...(input.stack ? { stack: input.stack } : {}), | ||
| ...(input.code ? { code: input.code } : {}), | ||
| ...(input.statusCode ? { statusCode: input.statusCode } : {}), | ||
| } | ||
| } | ||
|
|
||
| /** takes an error returns new error keeping some custom properties */ | ||
| function safeError (input) { | ||
reggi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const { message, errorType, ...data } = serializedError(input) | ||
| const output = new Error(message) | ||
| return Object.assign(output, { ...data, name: errorType }) | ||
| } | ||
|
|
||
| /** runs a function within try / catch and throws safeError */ | ||
| async function safeThrow (func) { | ||
| try { | ||
| return await func() | ||
| } catch (error) { | ||
| throw safeError(error) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| safeThrow, | ||
| serializedError, | ||
| safeError, | ||
| } | ||
This file contains hidden or 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 hidden or 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,135 @@ | ||
| const { serializedError, safeError, safeThrow } = require('../lib/error') | ||
| const t = require('tap') | ||
|
|
||
| class CustomError extends Error { | ||
| constructor (message, data) { | ||
| super(message) | ||
| this.sensitive = data | ||
| } | ||
| } | ||
|
|
||
| class NoMessageNoStackError extends Error { | ||
| constructor (message) { | ||
| super(message) | ||
| delete this.message | ||
| delete this.stack | ||
| } | ||
| } | ||
|
|
||
| t.test('serializedError', async t => { | ||
| await t.test('native error', async t => { | ||
| const exampleError = new Error('hello world') | ||
|
|
||
| t.same( | ||
| serializedError(exampleError), | ||
| { | ||
| errorType: 'Error', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('attached error', async t => { | ||
| const exampleError = new Error('hello world') | ||
| exampleError.code = 'E12345' | ||
| exampleError.statusCode = 404 | ||
|
|
||
| t.same( | ||
| serializedError(exampleError), | ||
| { | ||
| errorType: 'Error', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| code: 'E12345', | ||
| statusCode: 404, | ||
| }, | ||
| 'should serialize error with code / statusCode' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('assigned error', async t => { | ||
| const exampleError = new Error('hello world') | ||
| Object.assign(exampleError, { | ||
| code: 'E12345', | ||
| statusCode: 404, | ||
| }) | ||
|
|
||
| t.same( | ||
| serializedError(exampleError), | ||
| { | ||
| errorType: 'Error', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| code: 'E12345', | ||
| statusCode: 404, | ||
| }, | ||
| 'should serialize error with code / statusCode' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('custom error', async t => { | ||
| const exampleError = new CustomError('hello world', 'sensitive data') | ||
| t.same( | ||
| serializedError(exampleError), | ||
| { | ||
| errorType: 'CustomError', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('non-error', async t => { | ||
| const exampleError = 'hello world' | ||
| const result = serializedError(exampleError) | ||
| t.same(result.errorType, 'Error', 'should serialize error') | ||
| t.same(result.message, 'attempted to serialize a non-error object', 'should serialize message') | ||
| }) | ||
|
|
||
| await t.test('no-message no-stack', async t => { | ||
| const exampleError = new NoMessageNoStackError('hello world') | ||
| t.same( | ||
| serializedError(exampleError), | ||
| { | ||
| errorType: 'NoMessageNoStackError', | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| t.test('safeError', async t => { | ||
| await t.test('native error', async t => { | ||
| const badError = new Error('hello world') | ||
| Object.assign(badError, { | ||
| sensitive: 'sensitive data', | ||
| }) | ||
| const goodError = safeError(badError) | ||
|
|
||
| t.same(badError.sensitive, 'sensitive data', 'should have sensitive field') | ||
| t.same(goodError.sensitive, undefined, 'should not have sensitive field') | ||
| }) | ||
| await t.test('custom error', async t => { | ||
| const badError = new CustomError('hello world', 'sensitive data') | ||
| const goodError = safeError(badError) | ||
|
|
||
| t.same(badError.sensitive, 'sensitive data', 'should have sensitive field') | ||
| t.same(goodError.sensitive, undefined, 'should not have sensitive field') | ||
| }) | ||
| }) | ||
|
|
||
| t.test('safeThrow', async t => { | ||
| const badError = new CustomError('hello world', 'sensitive data') | ||
| t.same(badError.sensitive, 'sensitive data', 'should have sensitive field') | ||
| try { | ||
| await safeThrow(async () => { | ||
| throw badError | ||
| }) | ||
| t.fail('should throw') | ||
| } catch (goodError) { | ||
| t.same(goodError.sensitive, undefined, 'should not have sensitive field') | ||
| } | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.