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:
parent
ddb718f0bb
commit
56a1143e53
2 changed files with 72 additions and 7 deletions
|
@ -1,6 +1,7 @@
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const config = require('../../shared/config');
|
const config = require('../../shared/config');
|
||||||
const db = require('../data/db');
|
const db = require('../data/db');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
const LimitService = require('@tryghost/limit-service');
|
const LimitService = require('@tryghost/limit-service');
|
||||||
let limitService = new LimitService();
|
let limitService = new LimitService();
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ const init = () => {
|
||||||
|
|
||||||
const hostLimits = config.get('hostSettings:limits') || {};
|
const hostLimits = config.get('hostSettings:limits') || {};
|
||||||
|
|
||||||
|
try {
|
||||||
limitService.loadLimits({
|
limitService.loadLimits({
|
||||||
limits: hostLimits,
|
limits: hostLimits,
|
||||||
subscription,
|
subscription,
|
||||||
|
@ -31,6 +33,14 @@ const init = () => {
|
||||||
helpLink,
|
helpLink,
|
||||||
errors
|
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;
|
module.exports = limitService;
|
||||||
|
|
55
test/unit/server/services/limits.test.js
Normal file
55
test/unit/server/services/limits.test.js
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue