Skip to content

Commit 1a9ba44

Browse files
committed
Deleted everything with respect to HTMLconverter.convertWithDefault(). See #61 for future solution.
1 parent 34eed9a commit 1a9ba44

File tree

4 files changed

+19
-60
lines changed

4 files changed

+19
-60
lines changed

lib/converters/html-converter.js

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ module.exports = class HtmlConverter extends Converter {
3939
try {
4040
fileContent = await fs.readFile(htmlInfo.file, 'utf8');
4141
} catch (err) {
42-
if (htmlInfo && htmlInfo.file) {
43-
const error = new Error(`Reading the file "${htmlInfo.file ? htmlInfo.file : "(undefined)"}" failed.`);
44-
error.type = 'IO_READING_FAILED';
45-
reject(error);
46-
} else {
47-
reject(new Error(`Not given: htmlInfo.file.`));
48-
}
49-
return;
42+
const error = new Error(`Reading the file "${htmlInfo.file}" failed.`);
43+
error.type = 'IO_READING_FAILED';
44+
45+
reject(error);
5046
}
5147

5248
const content = frontMatter(fileContent);
@@ -106,31 +102,4 @@ module.exports = class HtmlConverter extends Converter {
106102
}
107103
});
108104
}
109-
110-
/**
111-
* This is a wrapper around convert(), that will render some default HTML if the expected conversion fails.
112-
*
113-
* Call this function in stead of convert(), to complete requests in error conditions.
114-
*
115-
* @param htmlInfo - The information about HTML (path, engine)
116-
* @param data - The JSON data that is used by the engine to substitute variables.
117-
*
118-
* @param message - {string} text to display inside a paragraph of the default HTML, if data cannot be converted as expected
119-
* @param logger - optional logger instance, logs conversion error that causes default HTML rendering
120-
*
121-
* @returns - see convert()
122-
*/
123-
static async convertWithDefault(htmlInfo, data, message, logger) {
124-
let html;
125-
try {
126-
html = await HtmlConverter.convert(htmlInfo, data);
127-
} catch (error) {
128-
if (logger) {
129-
logger.error('HTML conversion renders default HTML because: ' + error.message);
130-
}
131-
html = `<html lang="en"><head><meta charset="utf-8"></head><body><p>${message ? message : 'Default HTML contents - no message given'}</p></body></html>`;
132-
}
133-
return html;
134-
}
135-
136105
};

lib/handlers/content-negotiation-handler.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ const CN_TYPES = [
2828
* @type {module.ContentNegotiationHandler}
2929
*/
3030
module.exports = class ContentNegotiationHandler extends Handler {
31-
constructor(logger) {
31+
constructor() {
3232
super();
33-
34-
this.logger = logger;
3533
this.responseHandler = new ResponseHandler();
3634
}
3735

@@ -82,10 +80,8 @@ module.exports = class ContentNegotiationHandler extends Handler {
8280
}
8381
catch (error) {
8482
// If something goes wrong with the data reformatting, send html with error code 500
85-
if (this.logger) {
86-
this.logger.error('Content negotiation (reformatting) error: ' + error.message);
87-
}
88-
const html = await HtmlConverter.convertWithDefault(htmlInfo['500'], {}, 'Status: 500', this.logger);
83+
console.error('Content negotiation (reformatting) error: ' + error.message);
84+
const html = await HtmlConverter.convert(htmlInfo['500'], {});
8985
this.responseHandler.handle(res, 500, 'text/html')(html);
9086
}
9187
};

lib/handlers/request-handler.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = class RequestHandler extends Handler {
1717
super();
1818
this.graphQLLDHandler = graphQLLDHandler;
1919
this.responseHandler = new ResponseHandler();
20-
this.contentNegotiationHandler = new ContentNegotiationHandler(logger);
20+
this.contentNegotiationHandler = new ContentNegotiationHandler();
2121
this.postprocessHandler = new PostprocessHandler();
2222
this.logger = logger;
2323
}
@@ -44,7 +44,7 @@ module.exports = class RequestHandler extends Handler {
4444
} catch (e) {
4545
// If something goes wrong with applying the pipe modules, send status code 500
4646
console.error('Pipe module error: ' + e.message);
47-
const html = await HTMLConverter.convertWithDefault(htmlInfo['500'], {}, 'Status: 500', this.logger);
47+
const html = await HTMLConverter.convert(htmlInfo['500'], {});
4848
this.responseHandler.handle(res, 500, 'text/html')(html);
4949
}
5050
})
@@ -61,8 +61,14 @@ module.exports = class RequestHandler extends Handler {
6161
}
6262

6363
const handleResponse = this.responseHandler.handle(res, status, 'text/html');
64-
const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger);
65-
handleResponse(html);
64+
65+
if (!htmlInfo[String(status)]) {
66+
this.logger.warn(`No HTML template is defined for status code ${status}. Sending an empty string instead.`);
67+
handleResponse('');
68+
} else {
69+
const html = await HTMLConverter.convert(htmlInfo[String(status)], {});
70+
handleResponse(html);
71+
}
6672
})
6773
} else {
6874
// No GraphQL query is defined, so we just use the template without data.
@@ -73,12 +79,12 @@ module.exports = class RequestHandler extends Handler {
7379
} catch (err) {
7480
this.logger.error(`HTML conversion failed for "${req.path}".`);
7581
status = 500;
76-
const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger);
82+
const html = await HTMLConverter.convert(htmlInfo[String(status)], {});
7783
this.responseHandler.handle(res, status, 'text/html')(html);
7884
}
7985
}
8086
} else {
81-
const html = await HTMLConverter.convertWithDefault(htmlInfo[String(status)], {}, `Status: ${status}`, this.logger);
87+
const html = await HTMLConverter.convert(htmlInfo[String(status)], {});
8288
this.responseHandler.handle(res, status, 'text/html')(html);
8389
}
8490
}

test/converters/html-converter.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,5 @@ describe('HtmlConverter', function () {
3333
html.should.deep.equal(EX_3_OUTPUT);
3434
});
3535

36-
it('should be able to convert the given markdown with a layout to HTML', async () => {
37-
const html = await HtmlConverter.convert(EX_4_HTML_INFO, null);
38-
isHTML(html).should.be.true;
39-
html.should.deep.equal(EX_4_OUTPUT);
40-
});
41-
42-
it(`should be able to provide a default HTML, when calling convertWithDefault with input that otherwise can't be converted`, async () => {
43-
const testMessage = 'test-with-default';
44-
const html = await HtmlConverter.convertWithDefault(null, null, testMessage);
45-
isHTML(html).should.be.true;
46-
html.should.include(testMessage);
47-
});
4836
})
4937
});

0 commit comments

Comments
 (0)