refactor(logging): replace console usage in auth flows

This commit is contained in:
William Valentin
2025-09-23 10:15:57 -07:00
parent 6b6a44acef
commit c1c8e28f01
4 changed files with 104 additions and 32 deletions

View File

@@ -0,0 +1,27 @@
import { normalizeError } from '../error';
describe('normalizeError', () => {
it('returns the same error instance when provided', () => {
const error = new Error('test');
expect(normalizeError(error)).toBe(error);
});
it('converts strings into Error instances', () => {
const result = normalizeError('failure');
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('failure');
});
it('stringifies objects when creating the error message', () => {
const result = normalizeError({ reason: 'timeout', code: 504 });
expect(result.message).toBe('{"reason":"timeout","code":504}');
});
it('falls back to a generic error for unserializable input', () => {
const circular: Record<string, unknown> = {};
circular.self = circular;
const result = normalizeError(circular);
expect(result.message).toBe('Unknown error');
});
});

15
utils/error.ts Normal file
View File

@@ -0,0 +1,15 @@
export const normalizeError = (error: unknown): Error => {
if (error instanceof Error) {
return error;
}
if (typeof error === 'string') {
return new Error(error);
}
try {
return new Error(JSON.stringify(error));
} catch {
return new Error('Unknown error');
}
};