index.js
4.51 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Load modules
var Http = require('http');
var NodeUtil = require('util');
var Hoek = require('hoek');
// Declare internals
var internals = {};
exports = module.exports = internals.Boom = function (/* (new Error) or (code, message) */) {
var self = this;
Hoek.assert(this.constructor === internals.Boom, 'Error must be instantiated using new');
Error.call(this);
this.isBoom = true;
this.response = {
code: 0,
payload: {},
headers: {}
// type: 'content-type'
};
if (arguments[0] instanceof Error) {
// Error
var error = arguments[0];
this.data = error;
this.response.code = error.code || 500;
if (error.message) {
this.message = error.message;
}
}
else {
// code, message
var code = arguments[0];
var message = arguments[1];
Hoek.assert(!isNaN(parseFloat(code)) && isFinite(code) && code >= 400, 'First argument must be a number (400+)');
this.response.code = code;
if (message) {
this.message = message;
}
}
// Response format
this.reformat();
return this;
};
NodeUtil.inherits(internals.Boom, Error);
internals.Boom.prototype.reformat = function () {
this.response.payload.code = this.response.code;
this.response.payload.error = Http.STATUS_CODES[this.response.code] || 'Unknown';
if (this.message) {
this.response.payload.message = Hoek.escapeHtml(this.message); // Prevent XSS from error message
}
};
// Utilities
internals.Boom.badRequest = function (message) {
return new internals.Boom(400, message);
};
internals.Boom.unauthorized = function (message, scheme, attributes) { // Or function (message, wwwAuthenticate[])
var err = new internals.Boom(401, message);
if (!scheme) {
return err;
}
var wwwAuthenticate = '';
if (typeof scheme === 'string') {
// function (message, scheme, attributes)
wwwAuthenticate = scheme;
if (attributes) {
var names = Object.keys(attributes);
for (var i = 0, il = names.length; i < il; ++i) {
if (i) {
wwwAuthenticate += ',';
}
var value = attributes[names[i]];
if (value === null ||
value === undefined) { // Value can be zero
value = '';
}
wwwAuthenticate += ' ' + names[i] + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"';
}
}
if (message) {
if (attributes) {
wwwAuthenticate += ',';
}
wwwAuthenticate += ' error="' + Hoek.escapeHeaderAttribute(message) + '"';
}
else {
err.isMissing = true;
}
}
else {
// function (message, wwwAuthenticate[])
var wwwArray = scheme;
for (var i = 0, il = wwwArray.length; i < il; ++i) {
if (i) {
wwwAuthenticate += ', ';
}
wwwAuthenticate += wwwArray[i];
}
}
err.response.headers['WWW-Authenticate'] = wwwAuthenticate;
return err;
};
internals.Boom.clientTimeout = function (message) {
return new internals.Boom(408, message);
};
internals.Boom.serverTimeout = function (message) {
return new internals.Boom(503, message);
};
internals.Boom.forbidden = function (message) {
return new internals.Boom(403, message);
};
internals.Boom.notFound = function (message) {
return new internals.Boom(404, message);
};
internals.Boom.internal = function (message, data) {
var err = new internals.Boom(500, message);
if (data && data.stack) {
err.trace = data.stack.split('\n');
err.outterTrace = Hoek.displayStack(1);
}
else {
err.trace = Hoek.displayStack(1);
}
err.data = data;
err.response.payload.message = 'An internal server error occurred'; // Hide actual error from user
return err;
};
internals.Boom.passThrough = function (code, payload, contentType, headers) {
var err = new internals.Boom(500, 'Pass-through'); // 500 code is only used to initialize
err.data = {
code: code,
payload: payload,
type: contentType
};
err.response.code = code;
err.response.type = contentType;
err.response.headers = headers;
err.response.payload = payload;
return err;
};