mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Fixed error message when booting with no redirects
refs 91efa4605c
- When the instance is booted without any redirects files configured it's not supposed to error but rather default to an "empty" [] redirects configuration.
- Ideally the logic shoudl not contain try/catch block at all and fail as soon as there's any error during the initialization. This wasn't changed at this time due to possible break of existing Ghost instances
This commit is contained in:
parent
28bcd66d1b
commit
0ccf31cdb5
2 changed files with 33 additions and 7 deletions
|
@ -140,16 +140,22 @@ class CustomRedirectsAPI {
|
|||
}
|
||||
|
||||
async init() {
|
||||
// NOTE: the try/catch block here is due to possible breaking change for existing misconfigured
|
||||
// instances in the wild. Would be a good idea to remove it during v5 migration to enforce
|
||||
// fail-fast initialization.
|
||||
try {
|
||||
const filePath = await this.getRedirectsFilePath();
|
||||
const content = await readRedirectsFile(filePath);
|
||||
const ext = path.extname(filePath);
|
||||
const redirects = parseRedirectsFile(content, ext);
|
||||
validation.validate(redirects);
|
||||
this.redirectManager.removeAllRedirects();
|
||||
|
||||
for (const redirect of redirects) {
|
||||
this.redirectManager.addRedirect(redirect.from, redirect.to, {permanent: redirect.permanent});
|
||||
if (filePath !== null) {
|
||||
const content = await readRedirectsFile(filePath);
|
||||
const ext = path.extname(filePath);
|
||||
const redirects = parseRedirectsFile(content, ext);
|
||||
validation.validate(redirects);
|
||||
|
||||
this.redirectManager.removeAllRedirects();
|
||||
for (const redirect of redirects) {
|
||||
this.redirectManager.addRedirect(redirect.from, redirect.to, {permanent: redirect.permanent});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (errors.utils.isIgnitionError(err)) {
|
||||
|
|
|
@ -3,6 +3,7 @@ const sinon = require('sinon');
|
|||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const logging = require('@tryghost/logging');
|
||||
const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
|
||||
const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api');
|
||||
|
||||
|
@ -26,12 +27,31 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
beforeEach(function () {
|
||||
sinon.stub(fs, 'pathExists');
|
||||
sinon.stub(fs, 'readFile');
|
||||
sinon.spy(logging, 'error');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe('init', function () {
|
||||
it('initializes without errors when redirects file is not present', async function () {
|
||||
const redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath
|
||||
}, redirectManager);
|
||||
|
||||
await customRedirectsAPI.init();
|
||||
logging.error.called.should.be.false();
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('returns empty array if file does not exist', async function () {
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
|
||||
|
|
Loading…
Add table
Reference in a new issue