backend-app-api: include logId in filtered error log
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -195,6 +195,11 @@ describe('MiddlewareFactory', () => {
|
||||
it('should filter out internal errors', async () => {
|
||||
const app = express();
|
||||
|
||||
const grandChildLogger = {
|
||||
error: jest.fn(),
|
||||
};
|
||||
childLogger.child.mockReturnValue(grandChildLogger);
|
||||
|
||||
class DatabaseError extends Error {}
|
||||
const thrownError = new DatabaseError('some error');
|
||||
|
||||
@@ -205,18 +210,20 @@ describe('MiddlewareFactory', () => {
|
||||
|
||||
await request(app).get('/breaks');
|
||||
|
||||
expect(childLogger.error).toHaveBeenCalledTimes(2);
|
||||
const [{ logId }] = childLogger.child.mock.calls[0];
|
||||
|
||||
expect(logId).toMatch(/^[0-9a-f]+$/);
|
||||
expect(childLogger.error).toHaveBeenCalledWith(
|
||||
'Request failed with status 500',
|
||||
expect.objectContaining({
|
||||
message: expect.stringMatching(
|
||||
/^An internal error occurred logId=[0-9a-f]+$/,
|
||||
`An internal error occurred logId=${logId}`,
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(childLogger.error).toHaveBeenCalledWith(
|
||||
expect(grandChildLogger.error).toHaveBeenCalledWith(
|
||||
expect.stringMatching(
|
||||
/^Filtered internal error with logId=[0-9a-f]+ from response$/,
|
||||
`Filtered internal error with logId=${logId} from response`,
|
||||
),
|
||||
thrownError,
|
||||
);
|
||||
|
||||
@@ -20,10 +20,9 @@ import { randomBytes } from 'crypto';
|
||||
|
||||
function handleBadError(error: Error, logger: LoggerService) {
|
||||
const logId = randomBytes(10).toString('hex');
|
||||
logger.error(
|
||||
`Filtered internal error with logId=${logId} from response`,
|
||||
error,
|
||||
);
|
||||
logger
|
||||
.child({ logId })
|
||||
.error(`Filtered internal error with logId=${logId} from response`, error);
|
||||
const newError = new Error(`An internal error occurred logId=${logId}`);
|
||||
delete newError.stack; // Trim the stack since it's not particularly useful
|
||||
return newError;
|
||||
|
||||
Reference in New Issue
Block a user