-
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 8 commits
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,28 @@ | ||
| /** takes an error object and serializes it to a plan object */ | ||
| function serializeError (input) { | ||
| if (!(input instanceof Error)) { | ||
| if (typeof input === 'string') { | ||
| const error = new Error(`attempted to serialize a non-error, string String, "${input}"`) | ||
| return serializeError(error) | ||
| } | ||
| const error = new Error(`attempted to serialize a non-error, ${typeof input} ${input?.constructor?.name}`) | ||
| return serializeError(error) | ||
| } | ||
| // different error objects store status code differently | ||
| // AxiosError uses `status`, other services use `statusCode` | ||
| const statusCode = input.statusCode ?? input.status | ||
| // CAUTION: what we serialize here gets add to the size of logs | ||
| return { | ||
| errorType: input.errorType ?? input.constructor.name, | ||
| ...(input.message ? { message: input.message } : {}), | ||
| ...(input.stack ? { stack: input.stack } : {}), | ||
| // think of this as error code | ||
| ...(input.code ? { code: input.code } : {}), | ||
| // think of this as http status code | ||
| ...(statusCode ? { statusCode } : {}), | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| serializeError, | ||
| } |
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,131 @@ | ||
| const { serializeError } = 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('serializeError', async t => { | ||
| await t.test('native error', async t => { | ||
| const exampleError = new Error('hello world') | ||
|
|
||
| t.same( | ||
| serializeError(exampleError), | ||
| { | ||
| errorType: 'Error', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('attached error (status not statusCode)', async t => { | ||
| const exampleError = new Error('hello world') | ||
| exampleError.code = 'E12345' | ||
| exampleError.status = 404 | ||
|
|
||
| t.same( | ||
| serializeError(exampleError), | ||
| { | ||
| errorType: 'Error', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| code: 'E12345', | ||
| statusCode: 404, | ||
| }, | ||
| 'should serialize error with code / statusCode' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('attached error', async t => { | ||
| const exampleError = new Error('hello world') | ||
| exampleError.code = 'E12345' | ||
| exampleError.statusCode = 404 | ||
|
|
||
| t.same( | ||
| serializeError(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( | ||
| serializeError(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( | ||
| serializeError(exampleError), | ||
| { | ||
| errorType: 'CustomError', | ||
| message: 'hello world', | ||
| stack: exampleError.stack, | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('no-message no-stack', async t => { | ||
| const exampleError = new NoMessageNoStackError('hello world') | ||
| t.same( | ||
| serializeError(exampleError), | ||
| { | ||
| errorType: 'NoMessageNoStackError', | ||
| }, | ||
| 'should serialize error' | ||
| ) | ||
| }) | ||
|
|
||
| await t.test('undefined', async t => { | ||
| const results = serializeError(undefined) | ||
| t.same(results.errorType, 'Error', 'should serialize error') | ||
| t.same(results.message, 'attempted to serialize a non-error, undefined undefined', 'should serialize message') | ||
| }) | ||
|
|
||
| await t.test('date', async t => { | ||
| const results = serializeError(new Date()) | ||
| t.same(results.errorType, 'Error', 'should serialize error') | ||
| t.same(results.message, 'attempted to serialize a non-error, object Date', 'should serialize message') | ||
| }) | ||
|
|
||
| await t.test('string', async t => { | ||
| const results = serializeError('hello world') | ||
| t.same(results.errorType, 'Error', 'should serialize error') | ||
| t.same(results.message, 'attempted to serialize a non-error, string String, "hello world"', 'should serialize message') | ||
| }) | ||
| }) |
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,100 @@ | ||
| const t = require('tap') | ||
| const { serializeError } = require('../lib/error') | ||
| const { redact, redactError, redactThrow } = require('../lib/server') | ||
| const matchers = require('../lib/matchers') | ||
| const examples = require('./fixtures/examples') | ||
|
|
||
| t.test('redact', async t => { | ||
| await t.test('error has npm secret in message', async t => { | ||
| const error = new Error(`message contains npm secret: ${examples.NPM_SECRET.npm_36}`) | ||
| t.same(error.message, `message contains npm secret: ${examples.NPM_SECRET.npm_36}`) | ||
| const output = redact({ error }) | ||
| t.same(output, { | ||
| error: { | ||
| errorType: 'Error', | ||
| message: `message contains npm secret: ${matchers.NPM_SECRET.replacement}`, | ||
| stack: error.stack.replace(examples.NPM_SECRET.npm_36, matchers.NPM_SECRET.replacement), | ||
| }, | ||
| }) | ||
| }) | ||
| await t.test('error has npm secret in statusCode', async t => { | ||
| const x = new Error(`message`) | ||
| const error = Object.assign(x, { statusCode: examples.NPM_SECRET.npm_36 }) | ||
| const output = redact({ error }) | ||
| t.same(output, { | ||
| error: { | ||
| errorType: 'Error', | ||
| message: `message`, | ||
| stack: error.stack, | ||
| statusCode: matchers.NPM_SECRET.replacement, | ||
| }, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| class CustomError extends Error { | ||
| constructor (message, data) { | ||
| super(message) | ||
| this.sensitive = data | ||
| } | ||
| } | ||
|
|
||
| t.test('redactError', async t => { | ||
| await t.test('native error', async t => { | ||
| const badError = new Error('hello world') | ||
| Object.assign(badError, { | ||
| sensitive: 'sensitive data', | ||
| }) | ||
| const goodError = redactError(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 = redactError(badError) | ||
|
|
||
| t.same(badError.sensitive, 'sensitive data', 'should have sensitive field') | ||
| t.same(goodError.sensitive, undefined, 'should not have sensitive field') | ||
| }) | ||
| await t.test('redacts sensitive error.message', async t => { | ||
| const badError = new Error(`npm token: ${examples.NPM_SECRET.npm_36}`) | ||
| const goodError = redactError(badError) | ||
|
|
||
| t.same(badError.message, `npm token: ${examples.NPM_SECRET.npm_36}`, 'should have message') | ||
| t.same(goodError.message, `npm token: ${matchers.NPM_SECRET.replacement}`, 'should have message') | ||
| }) | ||
| }) | ||
|
|
||
| t.test('redactThrow', async t => { | ||
| await t.test('successfully throws error', async t => { | ||
| const badError = new CustomError('hello world', 'sensitive data') | ||
| t.same(badError.sensitive, 'sensitive data', 'should have sensitive field') | ||
| try { | ||
| await redactThrow(async () => { | ||
| throw badError | ||
| }) | ||
| t.fail('should throw') | ||
| } catch (goodError) { | ||
| t.same(goodError.sensitive, undefined, 'should not have sensitive field') | ||
| } | ||
| }) | ||
| t.test('invalid argument not function', async t => { | ||
| try { | ||
| await redactThrow('hello world') | ||
| t.fail('should throw') | ||
| } catch (error) { | ||
| t.same(error.message, 'redactThrow expects a function', 'should throw with correct message') | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| t.test('serialize a redactError', async t => { | ||
| const badError = new CustomError('hello world', 'sensitive data') | ||
| const goodError = redactError(badError) | ||
| const serialized = serializeError(goodError) | ||
| t.same(serialized.errorType, 'CustomError', 'should serialize error') | ||
| t.same(serialized.message, 'hello world', 'should serialize message') | ||
| t.same(serialized.stack, goodError.stack, 'should serialize stack') | ||
hashtagchris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| t.same(serialized.sensitive, undefined, 'should not serialize sensitive data') | ||
| }) | ||
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.