Skip to content

Commit

Permalink
Deleted everything with respect to HTMLconverter.convertWithDefault()…
Browse files Browse the repository at this point in the history
…. See #61 for future solution.
  • Loading branch information
mvanbrab committed Jan 25, 2021
1 parent 34eed9a commit 1a9ba44
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 60 deletions.
39 changes: 4 additions & 35 deletions lib/converters/html-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = `<html lang="en"><head><meta charset="utf-8"></head><body><p>${message ? message : 'Default HTML contents - no message given'}</p></body></html>`;
}
return html;
}

};
10 changes: 3 additions & 7 deletions lib/handlers/content-negotiation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
};
Expand Down
18 changes: 12 additions & 6 deletions lib/handlers/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
})
Expand All @@ -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.
Expand All @@ -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);
}
}
Expand Down
12 changes: 0 additions & 12 deletions test/converters/html-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})
});

0 comments on commit 1a9ba44

Please sign in to comment.