-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat: Support serializing primitives within Error causes (#158) #159
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,25 @@ test('serializes error causes', () => { | |
} | ||
}) | ||
|
||
test('serializes non Error error cause from constructor', () => { | ||
for (const cause of [ | ||
'string', | ||
42, | ||
// ['an', 'array'], | ||
// ['a', ['nested', 'array']], | ||
// { an: 'object' }, | ||
// { a: { nested: 'object' } }, | ||
Comment on lines
+69
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these commented out because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally, yes. I commented about a WIP approach I have locally in this comment. |
||
Symbol('symbol') | ||
]) { | ||
const err = Error('foo', { cause }) | ||
const serialized = serializer(err) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo: ' + String(cause)) | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.match(serialized.stack, /Error: foo/) | ||
} | ||
}) | ||
|
||
test('serializes error causes with VError support', function (t) { | ||
// Fake VError-style setup | ||
const err = Error('foo: bar') | ||
|
@@ -85,7 +104,7 @@ test('keeps non-error cause', () => { | |
err.cause = 'abc' | ||
const serialized = serializer(err) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.strictEqual(serialized.message, 'foo: abc') | ||
assert.strictEqual(serialized.cause, 'abc') | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test hints at why supporting non-Error objects is probably not a great idea. What should be done if someone threw?:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If cause is not an error then it should be serialized as a non error value would be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the point of this PR is to serialize non-error objects as error objects, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess an additional test is needed to vizualize the output, but really the point of this PR was to serialize non-error objects and display them as is, not to serialize them as Error objects specifically.
So if an object with nested objects was passed as a cause, regardless if its nested properties were named
cause
or something else, the entire object and its nested properties and values would be output.