0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Fixed incorrect usage of limit config causing Ghost not to boot

no issue

- When applying an incorrect limits config, or missing expected values, Ghost would not boot as the errors would interrupt this process, which should not happen
- This commit catches the error thrown by the limit-service on boot sequence and transforms it into a warning if it's an `IncorectUsageError`. Other errors are handled as before
- Added a test for the limit-service service
This commit is contained in:
Aileen Nowak 2022-05-10 14:23:00 -04:00 committed by Aileen Booker
parent ddb718f0bb
commit 56a1143e53
2 changed files with 72 additions and 7 deletions

View file

@ -1,6 +1,7 @@
const errors = require('@tryghost/errors');
const config = require('../../shared/config');
const db = require('../data/db');
const logging = require('@tryghost/logging');
const LimitService = require('@tryghost/limit-service');
let limitService = new LimitService();
@ -24,13 +25,22 @@ const init = () => {
const hostLimits = config.get('hostSettings:limits') || {};
limitService.loadLimits({
limits: hostLimits,
subscription,
db,
helpLink,
errors
});
try {
limitService.loadLimits({
limits: hostLimits,
subscription,
db,
helpLink,
errors
});
} catch (error) {
// Do not block the boot process for an incorrect usage error
if (error instanceof errors.IncorrectUsageError) {
logging.warn(error);
} else {
throw error;
}
}
};
module.exports = limitService;

View file

@ -0,0 +1,55 @@
const assert = require('assert');
const sinon = require('sinon');
const rewire = require('rewire');
const limits = rewire('../../../../core/server/services/limits');
const configUtils = require('../../../utils/configUtils');
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
describe('Limit Service Init', function () {
let loggerStub;
let limitServiceStub;
beforeEach(function () {
loggerStub = sinon.spy(logging);
limitServiceStub = sinon.stub();
limits.__set__('limitService.loadLimits', limitServiceStub);
configUtils.set({
hostSettings: {}
});
});
afterEach(function () {
configUtils.restore();
sinon.restore();
});
it('initiates and loads limits - minimal setup', async function () {
limitServiceStub.returns(Promise.resolve());
await limits.init();
sinon.assert.notCalled(loggerStub.warn);
});
it('handles limit-service incorrect usage errors gracefully with a warning', async function () {
limitServiceStub.throws(new errors.IncorrectUsageError('Incorrect limits'));
await limits.init();
sinon.assert.called(loggerStub.warn);
});
it('handles limit-service other errors with exit', async function () {
const thrownError = new errors.InternalServerError('Something went wrong');
limitServiceStub.throws(thrownError);
try {
await limits.init();
} catch (error) {
sinon.assert.notCalled(loggerStub.warn);
assert.deepEqual(error, thrownError);
}
});
});