-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequestErrorHandler.js
84 lines (73 loc) · 1.83 KB
/
requestErrorHandler.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
75
76
77
78
79
80
81
82
83
84
'use strict';
/*
* handle all request error here,
*
*/
const pug = require('pug');
const path = require('path');
const error502PugFn = pug.compileFile(path.join(__dirname, '../resource/502.pug'));
const certPugFn = pug.compileFile(path.join(__dirname, '../resource/cert_error.pug'));
/**
* get error content for certification issues
*/
function getCertErrorContent(error, fullUrl) {
let content;
const title = 'The connection is not private. ';
let explain = 'There are error with the certfication of the site.';
switch (error.code) {
case 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY': {
explain = 'The certfication of the site you are visiting is not issued by a known agency, '
+ 'It usually happenes when the cert is a self-signed one.</br>'
+ 'If you know and trust the site, you can run AnyProxy with option <strong>-ignore-unauthorized-ssl</strong> to continue.'
break;
}
default: {
explain = ''
break;
}
}
try {
content = certPugFn({
title: title,
explain: explain,
code: error.code
});
} catch (parseErro) {
content = error.stack;
}
return content;
}
/*
* get the default error content
*/
function getDefaultErrorCotent(error, fullUrl) {
let content;
try {
content = error502PugFn({
error,
url: fullUrl,
errorStack: error.stack.split(/\n/)
});
} catch (parseErro) {
content = error.stack;
}
return content;
}
/*
* get mapped error content for each error
*/
module.exports.getErrorContent = function (error, fullUrl) {
let content = '';
error = error || {};
switch (error.code) {
case 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY': {
content = getCertErrorContent(error, fullUrl);
break;
}
default: {
content = getDefaultErrorCotent(error, fullUrl);
break;
}
}
return content;
}