This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows to configure default (or custom) error pages
Through the start options we can configure custom error pages for 4xx or 5xx errors. If not, there are default pages loaded. Issue #17
- Loading branch information
Showing
9 changed files
with
195 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
html { | ||
height: 100%; | ||
} | ||
body { | ||
min-height: 100%; | ||
height: 100%; | ||
} | ||
* { | ||
font-family: Verdana, Arial, Tahoma, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-content: center; | ||
height: 100%; | ||
} | ||
.container .center-block { | ||
font-size: 18px; | ||
text-align: center; | ||
} | ||
.container .center-block strong { | ||
font-size: 50px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="center-block"> | ||
<div> | ||
<strong>404</strong> | ||
</div> | ||
<div>The page could not be found</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
html { height: 100%; } | ||
body { | ||
min-height: 100%; | ||
height: 100%; | ||
} | ||
* { | ||
font-family: Verdana, Tahoma, Arial, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-content: center; | ||
height: 100%; | ||
} | ||
.container .center-block { | ||
font-size: 18px; | ||
text-align: center; | ||
} | ||
.container .center-block strong { | ||
font-size: 50px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="center-block"> | ||
<div> | ||
<strong>500</strong> | ||
</div> | ||
<div>The page could not be found</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path') | ||
const CherryConfigurator = require('../abstract/CherryConfigurator') | ||
const check = require('../helpers/check') | ||
|
||
/** | ||
* The configurator of the default error pages | ||
* Inherits from the abstract CherryConfigurator | ||
*/ | ||
class DefaultErrorPageConfigurator extends CherryConfigurator { | ||
constructor () { | ||
super({}) | ||
|
||
this.manager.clientErrorPage = this.clientErrorPage | ||
this.manager.serverErrorPage = this.serverErrorPage | ||
} | ||
|
||
/** | ||
* Configure the default pages process in case of error | ||
* @param {Object} options The options set to the cherry instance | ||
*/ | ||
configure (options) { | ||
if (check.isDefined(options, 'defaultPages')) { | ||
Object.keys(this.manager).forEach((defaultPage) => { | ||
if (check.isDefined(options.defaultPages, defaultPage)) { | ||
this.manager[defaultPage] = options.defaultPages[defaultPage] | ||
} | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* The default process in case of a 4xx page | ||
* @param {CherryIncomingMessage} req The current request | ||
* @param {CherryServerResponse} res The response object | ||
*/ | ||
clientErrorPage (req, res) { | ||
res.html(path.join(__dirname, '../assets/default_pages/4xx.html'), { statusCode: 404 }) | ||
} | ||
|
||
/** | ||
* The default process in case of a 5xx page | ||
* @param {CherryIncomingMessage} req The current request | ||
* @param {CherryServerResponse} res The response object | ||
* @param {Error} err The error catch | ||
*/ | ||
serverErrorPage (req, res, err) { | ||
res.html(path.join(__dirname, '../assets/default_pages/5xx.html'), { statusCode: 500 }) | ||
} | ||
} | ||
|
||
module.exports = DefaultErrorPageConfigurator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/specs/configuration/DefaultErrorPageConfigurator.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const DefaultErrorPageConfigurator = require(path.join(__root, './src/configuration/DefaultErrorPageConfigurator')) | ||
|
||
const default404 = () => { | ||
return 404 | ||
} | ||
let defaultErrorPageConfigurator = null | ||
let customDefaultErrorPageConfigurator = null | ||
const fakeResponse = { | ||
errorCode: 0, | ||
html: function (path, options) { | ||
this.errorCode = options.statusCode | ||
} | ||
} | ||
|
||
describe('DefaultErrorPageConfigurator', () => { | ||
before(() => { | ||
defaultErrorPageConfigurator = new DefaultErrorPageConfigurator() | ||
customDefaultErrorPageConfigurator = new DefaultErrorPageConfigurator() | ||
}) | ||
|
||
it('Tests the method configure', () => { | ||
customDefaultErrorPageConfigurator.configure({ | ||
defaultPages: { | ||
clientErrorPage: default404 | ||
} | ||
}) | ||
}) | ||
|
||
it('Tests the method clientErrorPage', () => { | ||
defaultErrorPageConfigurator.manager.clientErrorPage({}, fakeResponse) | ||
expect(fakeResponse.errorCode).to.be.equal(404) | ||
expect(customDefaultErrorPageConfigurator.manager.clientErrorPage({}, fakeResponse)).to.be.equal(404) | ||
}) | ||
|
||
it('Tests the method serverErrorPage', () => { | ||
defaultErrorPageConfigurator.manager.serverErrorPage({}, fakeResponse) | ||
expect(fakeResponse.errorCode).to.be.equal(500) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters