Skip to content

Commit 1ac6c45

Browse files
committed
Handling unexpected error in publish pipeline.
1 parent b6e0336 commit 1ac6c45

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/lib/logger.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,22 @@ function isSensitiveKey(key: string): boolean {
5656

5757
/**
5858
* Recursively sanitize a value, replacing sensitive fields with '***'.
59-
* Handles objects, arrays, and inline bearer tokens in strings.
59+
* Handles objects, arrays, inline bearer tokens in strings, and Error objects.
6060
*/
6161
function sanitize(value: unknown): unknown {
6262
if (typeof value === 'string') {
6363
// Redact inline bearer tokens (e.g. "Bearer eyJ...")
6464
return value.replace(/Bearer\s+[A-Za-z0-9\-._~+/]+=*/gi, 'Bearer ***');
6565
}
66+
if (value instanceof Error) {
67+
// Convert Error objects to a serializable form
68+
return {
69+
name: value.name,
70+
message: value.message,
71+
stack: value.stack,
72+
...(value.cause ? { cause: sanitize(value.cause) } : {}),
73+
};
74+
}
6675
if (Array.isArray(value)) {
6776
return value.map((item) => sanitize(item));
6877
}

tests/unit/lib/logger.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ describe('Logger', () => {
146146
expect(output).toContain('test');
147147
expect(output).toContain('42');
148148
});
149+
150+
it('should serialize Error objects with message and stack', () => {
151+
const error = new Error('Something went wrong');
152+
loggerInstance.error('Error occurred:', error);
153+
const output = stderrSpy.mock.calls[0]![0] as string;
154+
expect(output).toContain('Something went wrong');
155+
expect(output).toContain('"name":"Error"');
156+
expect(output).toContain('"message":"Something went wrong"');
157+
// Stack trace should be present (not testing exact format)
158+
expect(output).toContain('"stack"');
159+
});
160+
161+
it('should serialize Error objects with cause', () => {
162+
const innerError = new Error('Inner error');
163+
const outerError = new Error('Outer error', { cause: innerError });
164+
loggerInstance.error('Nested error:', outerError);
165+
const output = stderrSpy.mock.calls[0]![0] as string;
166+
expect(output).toContain('Outer error');
167+
expect(output).toContain('Inner error');
168+
expect(output).toContain('"cause"');
169+
});
149170
});
150171

151172
describe('non-ASCII punctuation normalization', () => {

0 commit comments

Comments
 (0)