1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

fix: log missing user at warn level (#3735)

When using PATs if the user that the PAT is for has been removed, we
currently log the missing user at ERROR level. Since this is not
something our SREs can fix, this PR downgrades the NotFoundError to
WARN, instead of ERROR.
This commit is contained in:
Christopher Kolstad 2023-05-10 13:31:42 +02:00 committed by GitHub
parent 9c395596a5
commit af3944bd75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import getLogger from '../../test/fixtures/no-logger';
import patMiddleware from './pat-middleware';
import User from '../types/user';
import NotFoundError from '../error/notfound-error';
let config: any;
@ -108,3 +109,37 @@ test('should call next if accountService throws exception', async () => {
expect(cb).toHaveBeenCalled();
getLogger.setMuteError(false);
});
test('Should not log at error level if user not found', async () => {
let fakeLogger = {
debug: () => {},
info: () => {},
warn: jest.fn(),
error: jest.fn(),
fatal: console.error,
};
const conf = {
getLogger: () => {
return fakeLogger;
},
flagResolver: {
isEnabled: jest.fn().mockReturnValue(true),
},
};
const accountService = {
getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => {
throw new NotFoundError('Could not find pat');
}),
};
let mw = patMiddleware(conf, { accountService });
const cb = jest.fn();
const req = {
header: jest.fn().mockReturnValue('user:some-token'),
user: undefined,
};
await mw(req, undefined, cb);
expect(fakeLogger.error).not.toHaveBeenCalled();
expect(fakeLogger.warn).toHaveBeenCalled();
});

View File

@ -1,5 +1,6 @@
import { IUnleashConfig } from '../types';
import { IAuthRequest } from '../routes/unleash-types';
import NotFoundError from '../error/notfound-error';
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const patMiddleware = (
@ -21,7 +22,14 @@ const patMiddleware = (
accountService.addPATSeen(apiToken);
}
} catch (error) {
logger.error(error);
if (error instanceof NotFoundError) {
logger.warn(
'Tried to use a PAT token for user that no longer existed',
error,
);
} else {
logger.error(error);
}
}
next();
};