forked from eduardoboucas/staticman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorHandler.js
74 lines (57 loc) · 2.13 KB
/
ErrorHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const path = require('path')
const config = require(path.join(__dirname, '/../config'))
const ErrorHandler = function () {
this.ERROR_MESSAGES = {
'missing-input-secret': 'reCAPTCHA: The secret parameter is missing',
'invalid-input-secret': 'reCAPTCHA: The secret parameter is invalid or malformed',
'missing-input-response': 'reCAPTCHA: The response parameter is missing',
'invalid-input-response': 'reCAPTCHA: The response parameter is invalid or malformed',
'RECAPTCHA_MISSING_CREDENTIALS': 'Missing reCAPTCHA API credentials',
'RECAPTCHA_FAILED_DECRYPT': 'Could not decrypt reCAPTCHA secret',
'RECAPTCHA_CONFIG_MISMATCH': 'reCAPTCHA options do not match Staticman config',
'PARSING_ERROR': 'Error whilst parsing config file',
'GITHUB_AUTH_TOKEN_MISSING': 'The site requires a valid GitHub authentication token to be supplied in the `options[github-token]` field'
}
this.ERROR_CODE_ALIASES = {
'missing-input-secret': 'RECAPTCHA_MISSING_INPUT_SECRET',
'invalid-input-secret': 'RECAPTCHA_INVALID_INPUT_SECRET',
'missing-input-response': 'RECAPTCHA_MISSING_INPUT_RESPONSE',
'invalid-input-response': 'RECAPTCHA_INVALID_INPUT_RESPONSE'
}
}
ErrorHandler.prototype.getErrorCode = function (error) {
return this.ERROR_CODE_ALIASES[error] || error
}
ErrorHandler.prototype.getMessage = function (error) {
return this.ERROR_MESSAGES[error]
}
ErrorHandler.prototype.log = function (err, instance) {
let parameters = {}
let prefix = ''
if (instance) {
parameters = instance.getParameters()
prefix += `${parameters.username}/${parameters.repository}`
}
console.log(`${prefix}`, err)
}
ErrorHandler.prototype._save = function (errorCode, data) {
data = data || {}
if (data.err) {
data.err._smErrorCode = data.err._smErrorCode || errorCode
return data.err
}
let payload = {
_smErrorCode: errorCode
}
if (data.data) {
payload.data = data.data
}
return payload
}
const errorHandler = new ErrorHandler()
module.exports = function () {
return errorHandler._save.apply(errorHandler, arguments)
}
module.exports.getInstance = function () {
return errorHandler
}