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

Added output to the get helper when the timeout is exceeded

refs https://linear.app/tryghost/issue/ENG-670
This commit is contained in:
Fabien O'Carroll 2024-02-27 09:35:32 -05:00
parent ac8eb37367
commit 52726aa744
2 changed files with 15 additions and 5 deletions

View file

@ -2,7 +2,7 @@
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
// Fetches data from the API
const {config, api, prepareContextResource} = require('../services/proxy');
const {hbs} = require('../services/handlebars');
const {hbs, SafeString} = require('../services/handlebars');
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
@ -154,7 +154,7 @@ async function makeAPICall(resource, controllerName, action, apiOptions) {
}
}));
resolve({[resource]: []});
resolve({[resource]: [], '@@ABORTED_GET_HELPER@@': true});
}, threshold);
});
@ -242,10 +242,16 @@ module.exports = async function get(resource, options) {
}
// Call the main template function
return options.fn(response, {
const rendered = options.fn(response, {
data: data,
blockParams: blockParams
});
if (response['@@ABORTED_GET_HELPER@@']) {
return new SafeString(`<span data-aborted-get-helper>Could not load content</span>` + rendered);
} else {
return rendered;
}
});
return result;
} catch (error) {

View file

@ -1,3 +1,4 @@
const assert = require('assert');
const should = require('should');
const sinon = require('sinon');
const {SafeString} = require('../../../../core/frontend/services/handlebars');
@ -21,7 +22,9 @@ describe('{{#get}} helper', function () {
});
beforeEach(function () {
fn = sinon.spy();
fn = sinon.spy(function () {
return '';
});
inverse = sinon.spy();
locals = {root: {}, globalProp: {foo: 'bar'}};
@ -394,12 +397,13 @@ describe('{{#get}} helper', function () {
it('should log an error and return safely if it hits the timeout threshold', async function () {
configUtils.set('optimization:getHelper:timeout:threshold', 1);
await get.call(
const result = await get.call(
{},
'posts',
{hash: {}, data: locals, fn: fn, inverse: inverse}
);
assert(result.toString().includes('data-aborted-get-helper'));
// A log message will be output
logging.error.calledOnce.should.be.true();
// The get helper gets called with an empty array of results