This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* DO not use arrow functions for cases not binding this * Add logging framework to Thimble * Add error display/reporting framework * Use error framework to log meaningful error messages * Fix JSHint errors * Review fixes
- Loading branch information
1 parent
f404b3d
commit bc54870
Showing
33 changed files
with
806 additions
and
291 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
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
This file was deleted.
Oops, something went wrong.
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,71 @@ | ||
"use strict"; | ||
|
||
const Nunjucks = require("nunjucks"); | ||
|
||
class HttpError { | ||
static generic(error, request, response, next) { | ||
//jshint unused:vars | ||
const localeInfo = request.localeInfo; | ||
const locale = (localeInfo && localeInfo.locale) || "en-US"; | ||
const statusCode = response.statusCode || 500; | ||
let statusMessage = statusCode; | ||
const statusMessageKey = HttpError.getStatusMessageKey(statusCode); | ||
|
||
if(statusMessageKey) { | ||
statusMessage = Nunjucks.renderString( | ||
request.gettext(statusMessageKey, locale), | ||
{ httpStatusCode: statusCode } | ||
); | ||
} | ||
|
||
const userFriendlyError = { | ||
statusMessage, | ||
status: statusCode, | ||
message: error.userMessage || error | ||
}; | ||
|
||
response.format({ | ||
text() { | ||
response.send(`${userFriendlyError.statusMessage}: ${userFriendlyError.message}`); | ||
}, | ||
html() { | ||
response.render("error.html", userFriendlyError); | ||
}, | ||
json() { | ||
response.send(userFriendlyError); | ||
}, | ||
default() { | ||
response.send(userFriendlyError); | ||
} | ||
}); | ||
|
||
request.log.error(error); | ||
} | ||
|
||
static notFound(request, response, next) { | ||
//jshint unused:vars | ||
response.status(404); | ||
response.render("error.html", { status: 404 }); | ||
} | ||
|
||
static getStatusMessageKey(statusCode) { | ||
return statusCode >= 500 ? "errorHttpServiceUnavailable" | ||
: (statusCode >= 405 || statusCode === 400) ? "errorHttpClientError" | ||
: statusCode === 404 ? "errorHttpNotFound" | ||
: statusCode >= 401 ? "errorHttpAuthenticationFailed" | ||
: null; | ||
} | ||
|
||
static format(error, request) { | ||
const localeInfo = request.localeInfo; | ||
const locale = (localeInfo && localeInfo.locale) || "en-US"; | ||
const userMessageKey = request.errorMessageKey; | ||
|
||
error.userMessage = request.gettext(userMessageKey, locale); | ||
error.locale = localeInfo && localeInfo.lang; | ||
|
||
return error; | ||
} | ||
} | ||
|
||
module.exports = HttpError; |
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
Oops, something went wrong.