From 1a9ba441b1be0947673576ce55bf81cd5575c851 Mon Sep 17 00:00:00 2001 From: Martin Vanbrabant Date: Mon, 25 Jan 2021 17:09:52 +0100 Subject: [PATCH] Deleted everything with respect to HTMLconverter.convertWithDefault(). See #61 for future solution. --- lib/converters/html-converter.js | 39 +++------------------ lib/handlers/content-negotiation-handler.js | 10 ++---- lib/handlers/request-handler.js | 18 ++++++---- test/converters/html-converter.js | 12 ------- 4 files changed, 19 insertions(+), 60 deletions(-) diff --git a/lib/converters/html-converter.js b/lib/converters/html-converter.js index 6dc341bf..eadbfe7d 100644 --- a/lib/converters/html-converter.js +++ b/lib/converters/html-converter.js @@ -39,14 +39,10 @@ module.exports = class HtmlConverter extends Converter { try { fileContent = await fs.readFile(htmlInfo.file, 'utf8'); } catch (err) { - if (htmlInfo && htmlInfo.file) { - const error = new Error(`Reading the file "${htmlInfo.file ? htmlInfo.file : "(undefined)"}" failed.`); - error.type = 'IO_READING_FAILED'; - reject(error); - } else { - reject(new Error(`Not given: htmlInfo.file.`)); - } - return; + const error = new Error(`Reading the file "${htmlInfo.file}" failed.`); + error.type = 'IO_READING_FAILED'; + + reject(error); } const content = frontMatter(fileContent); @@ -106,31 +102,4 @@ module.exports = class HtmlConverter extends Converter { } }); } - - /** - * This is a wrapper around convert(), that will render some default HTML if the expected conversion fails. - * - * Call this function in stead of convert(), to complete requests in error conditions. - * - * @param htmlInfo - The information about HTML (path, engine) - * @param data - The JSON data that is used by the engine to substitute variables. - * - * @param message - {string} text to display inside a paragraph of the default HTML, if data cannot be converted as expected - * @param logger - optional logger instance, logs conversion error that causes default HTML rendering - * - * @returns - see convert() - */ - static async convertWithDefault(htmlInfo, data, message, logger) { - let html; - try { - html = await HtmlConverter.convert(htmlInfo, data); - } catch (error) { - if (logger) { - logger.error('HTML conversion renders default HTML because: ' + error.message); - } - html = `

${message ? message : 'Default HTML contents - no message given'}

`; - } - return html; - } - }; diff --git a/lib/handlers/content-negotiation-handler.js b/lib/handlers/content-negotiation-handler.js index 9a0c949b..3937e31c 100644 --- a/lib/handlers/content-negotiation-handler.js +++ b/lib/handlers/content-negotiation-handler.js @@ -28,10 +28,8 @@ const CN_TYPES = [ * @type {module.ContentNegotiationHandler} */ module.exports = class ContentNegotiationHandler extends Handler { - constructor(logger) { + constructor() { super(); - - this.logger = logger; this.responseHandler = new ResponseHandler(); } @@ -82,10 +80,8 @@ module.exports = class ContentNegotiationHandler extends Handler { } catch (error) { // If something goes wrong with the data reformatting, send html with error code 500 - if (this.logger) { - this.logger.error('Content negotiation (reformatting) error: ' + error.message); - } - const html = await HtmlConverter.convertWithDefault(htmlInfo['500'], {}, 'Status: 500', this.logger); + console.error('Content negotiation (reformatting) error: ' + error.message); + const html = await HtmlConverter.convert(htmlInfo['500'], {}); this.responseHandler.handle(res, 500, 'text/html')(html); } }; diff --git a/lib/handlers/request-handler.js b/lib/handlers/request-handler.js index 8cc71cb1..bb486e11 100644 --- a/lib/handlers/request-handler.js +++ b/lib/handlers/request-handler.js @@ -17,7 +17,7 @@ module.exports = class RequestHandler extends Handler { super(); this.graphQLLDHandler = graphQLLDHandler; this.responseHandler = new ResponseHandler(); - this.contentNegotiationHandler = new ContentNegotiationHandler(logger); + this.contentNegotiationHandler = new ContentNegotiationHandler(); this.postprocessHandler = new PostprocessHandler(); this.logger = logger; } @@ -44,7 +44,7 @@ module.exports = class RequestHandler extends Handler { } catch (e) { // If something goes wrong with applying the pipe modules, send status code 500 console.error('Pipe module error: ' + e.message); - const html = await HTMLConverter.convertWithDefault(htmlInfo['500'], {}, 'Status: 500', this.logger); + const html = await HTMLConverter.convert(htmlInfo['500'], {}); this.responseHandler.handle(res, 500, 'text/html')(html); } }) @@ -61,8 +61,14 @@ module.exports = class RequestHandler extends Handler { } const handleResponse = this.responseHandler.handle(res, status, 'text/html'); - const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger); - handleResponse(html); + + if (!htmlInfo[String(status)]) { + this.logger.warn(`No HTML template is defined for status code ${status}. Sending an empty string instead.`); + handleResponse(''); + } else { + const html = await HTMLConverter.convert(htmlInfo[String(status)], {}); + handleResponse(html); + } }) } else { // No GraphQL query is defined, so we just use the template without data. @@ -73,12 +79,12 @@ module.exports = class RequestHandler extends Handler { } catch (err) { this.logger.error(`HTML conversion failed for "${req.path}".`); status = 500; - const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger); + const html = await HTMLConverter.convert(htmlInfo[String(status)], {}); this.responseHandler.handle(res, status, 'text/html')(html); } } } else { - const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger); + const html = await HTMLConverter.convert(htmlInfo[String(status)], {}); this.responseHandler.handle(res, status, 'text/html')(html); } } diff --git a/test/converters/html-converter.js b/test/converters/html-converter.js index ddf42561..cfaa1da5 100644 --- a/test/converters/html-converter.js +++ b/test/converters/html-converter.js @@ -33,17 +33,5 @@ describe('HtmlConverter', function () { html.should.deep.equal(EX_3_OUTPUT); }); - it('should be able to convert the given markdown with a layout to HTML', async () => { - const html = await HtmlConverter.convert(EX_4_HTML_INFO, null); - isHTML(html).should.be.true; - html.should.deep.equal(EX_4_OUTPUT); - }); - - it(`should be able to provide a default HTML, when calling convertWithDefault with input that otherwise can't be converted`, async () => { - const testMessage = 'test-with-default'; - const html = await HtmlConverter.convertWithDefault(null, null, testMessage); - isHTML(html).should.be.true; - html.should.include(testMessage); - }); }) });