Removed i18n dependency from update check constructor

refs https://github.com/TryGhost/Team/issues/727

- The latest code standards require using tpl module in combination with local messages object instead of i18n  injected in the constructor
This commit is contained in:
Naz 2021-07-02 18:46:31 +04:00
parent 7ea81dc50d
commit 98a64ed0f8
3 changed files with 30 additions and 15 deletions

View File

@ -4,11 +4,17 @@ const crypto = require('crypto');
const moment = require('moment');
const Promise = require('bluebird');
const exec = require('child_process').exec;
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const debug = require('ghost-ignition').debug('update-check');
const internal = {context: {internal: true}};
const messages = {
checkingForUpdatesFailedError: 'Checking for updates failed, your site will continue to function.',
checkingForUpdatesFailedHelp: 'If you get this error repeatedly, please seek help from {url}'
};
/**
* Update Checker Class
*
@ -44,10 +50,9 @@ class UpdateCheckService {
* @param {Function} options.request - a HTTP request proxy function
* @param {Function} options.sendEmail - function handling sending an email
*/
constructor({api, config, i18n, logging, request, sendEmail}) {
constructor({api, config, logging, request, sendEmail}) {
this.api = api;
this.config = config;
this.i18n = i18n;
this.logging = logging;
this.request = request;
this.sendEmail = sendEmail;
@ -78,8 +83,8 @@ class UpdateCheckService {
}]
}, internal);
err.context = this.i18n.t('errors.updateCheck.checkingForUpdatesFailed.error');
err.help = this.i18n.t('errors.updateCheck.checkingForUpdatesFailed.help', {url: 'https://ghost.org/docs/'});
err.context = tpl(messages.checkingForUpdatesFailedError);
err.help = tpl(messages.checkingForUpdatesFailedHelp, {url: 'https://ghost.org/docs/'});
this.logging.error(err);
}

View File

@ -25,6 +25,7 @@
},
"dependencies": {
"@tryghost/errors": "^0.2.11",
"@tryghost/tpl": "^0.1.3",
"bluebird": "3.7.2",
"lodash": "4.17.21",
"moment": "2.24.0"

View File

@ -9,7 +9,6 @@ const UpdateCheckService = require('../lib/update-check-service');
describe('Update Check', function () {
const internal = {context: {internal: true}};
let settingsStub;
let i18nStub;
let loggingStub;
let requestStub;
@ -28,10 +27,6 @@ describe('Update Check', function () {
}]
});
i18nStub = {
t: sinon.stub()
};
loggingStub = {
error: sinon.stub()
};
@ -64,7 +59,6 @@ describe('Update Check', function () {
isPrivacyDisabled: true,
ghostVersion: '0.8.0'
},
i18n: i18nStub,
logging: loggingStub,
request: requestStub
});
@ -125,7 +119,6 @@ describe('Update Check', function () {
isPrivacyDisabled: true,
ghostVersion: '5.3.4'
},
i18n: i18nStub,
logging: loggingStub,
request: requestStub
});
@ -172,7 +165,6 @@ describe('Update Check', function () {
databaseType: 'mysql',
ghostVersion: '4.0.0'
},
i18n: i18nStub,
logging: loggingStub,
request: requestStub,
ghostMailer: {
@ -243,7 +235,6 @@ describe('Update Check', function () {
config: {
siteUrl: 'https://localhost:2368/test'
},
i18n: i18nStub,
logging: loggingStub,
request: sinon.stub().resolves({
body: {
@ -307,7 +298,6 @@ describe('Update Check', function () {
config: {
siteUrl: 'http://127.0.0.1:2369'
},
i18n: i18nStub,
logging: loggingStub,
request: sinon.stub().resolves({
body: [notification]
@ -355,7 +345,6 @@ describe('Update Check', function () {
config: {
siteUrl: 'https://localhost:2368/test'
},
i18n: i18nStub,
logging: loggingStub,
request: sinon.stub().resolves({
body: {
@ -369,4 +358,24 @@ describe('Update Check', function () {
notificationsAPIAddStub.calledOnce.should.equal(false);
});
});
describe('Error handling', function () {
it('logs an error when error', function () {
const updateCheckService = new UpdateCheckService({
api: {
settings: {
edit: settingsStub
}
},
logging: loggingStub
});
updateCheckService.updateCheckError({});
settingsStub.called.should.be.true();
loggingStub.error.called.should.be.true();
loggingStub.error.args[0][0].context.should.equal('Checking for updates failed, your site will continue to function.');
loggingStub.error.args[0][0].help.should.equal('If you get this error repeatedly, please seek help from https://ghost.org/docs/');
});
});
});