Skip to content

Commit 1c1ec84

Browse files
committed
add comments
1 parent 7f5625e commit 1c1ec84

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

lib/error-handler.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@ class ErrorHandler {
1818
this.links = Array.isArray(links) ? links : [links]
1919
}
2020

21+
/**
22+
* Creates Google and Stack Overflow links
23+
* which allow the user to search for
24+
* help on these platforms.
25+
*
26+
* @returns {Array}
27+
*/
2128
defaultLinks () {
2229
return [
2330
(error) => this.google(error),
2431
(error) => this.stackOverflow(error)
2532
]
2633
}
2734

35+
/**
36+
* Resolve a Google link with
37+
* the error’s message.
38+
*
39+
* @param {Object} error
40+
*
41+
* @returns {String}
42+
*/
2843
google (error) {
2944
return `
3045
<a
@@ -35,6 +50,14 @@ class ErrorHandler {
3550
</a>`
3651
}
3752

53+
/**
54+
* Resolve a Stack Overflow link
55+
* with the error’s message.
56+
*
57+
* @param {Object} error
58+
*
59+
* @returns {String}
60+
*/
3861
stackOverflow (error) {
3962
return `
4063
<a
@@ -45,14 +68,38 @@ class ErrorHandler {
4568
</a>`
4669
}
4770

71+
/**
72+
* Read the icon from disk.
73+
*
74+
* @param {String} name
75+
*
76+
* @returns {String}
77+
*/
4878
resolveIcon (name) {
4979
return Fs.readFileSync(this.resolveIconPath(name))
5080
}
5181

82+
/**
83+
* Resolve the icon’s path on the disk.
84+
*
85+
* @param {String} name
86+
*
87+
* @returns {String}
88+
*/
5289
resolveIconPath (name) {
5390
return Path.resolve(__dirname, 'icons', `${name}.svg`)
5491
}
5592

93+
/**
94+
* Check the outgoing response whether it’s
95+
* a developer error. If yes, show the
96+
* error details view.
97+
*
98+
* @param {Request} request
99+
* @param {Toolkit} h
100+
*
101+
* @returns {Response}
102+
*/
56103
async handle (request, h) {
57104
if (this.isDeveloperError(request.response)) {
58105
return this.resolveError(request, h)
@@ -61,10 +108,25 @@ class ErrorHandler {
61108
return h.continue
62109
}
63110

111+
/**
112+
* Check whether the `error` is a 500 error
113+
*
114+
* @param {Object} error
115+
*
116+
* @returns {Boolean}
117+
*/
64118
isDeveloperError (error) {
65119
return error.isBoom && error.output.statusCode === 500
66120
}
67121

122+
/**
123+
* Resolve the error and format, JSON or HTML.
124+
*
125+
* @param {Request} request
126+
* @param {Toolkit} h
127+
*
128+
* @returns {Response}
129+
*/
68130
async resolveError (request, h) {
69131
await this.logToTerminal(request)
70132

@@ -79,6 +141,11 @@ class ErrorHandler {
79141
return this.sendHtml(request, h)
80142
}
81143

144+
/**
145+
* Logs the error to terminal.
146+
*
147+
* @param {Request} request
148+
*/
82149
async logToTerminal (request) {
83150
if (this.toTerminal) {
84151
const youch = this.createYouch(request)
@@ -88,6 +155,15 @@ class ErrorHandler {
88155
}
89156
}
90157

158+
/**
159+
* Create a Youch instance to render a
160+
* detailed error view or serialize
161+
* the error to JSON.
162+
*
163+
* @param {Request} request
164+
*
165+
* @returns {Object}
166+
*/
91167
createYouch (request) {
92168
const error = request.response
93169
error.status = error.output.statusCode
@@ -104,16 +180,40 @@ class ErrorHandler {
104180
}
105181
}
106182

183+
/**
184+
* Determines whether the request
185+
* expects JSON in response.
186+
*
187+
* @param {Request} request
188+
*
189+
* @returns {Boolean}
190+
*/
107191
wantsJson (request) {
108192
const { 'user-agent': agent, accept } = request.raw.req.headers
109193

110194
return this.matches(agent, /curl|wget|postman|insomnia/i) || this.matches(accept, /json/)
111195
}
112196

197+
/**
198+
* Determine whether the given `str`
199+
* matches the `regex`.
200+
*
201+
* @param {String} str
202+
* @param {String} regex
203+
*
204+
* @returns {Boolean}
205+
*/
113206
matches (str, regex) {
114207
return str && str.match(regex)
115208
}
116209

210+
/**
211+
* Create an error object.
212+
*
213+
* @param {Request} request
214+
*
215+
* @returns {Object}
216+
*/
117217
composeError (request) {
118218
const error = request.response
119219

@@ -129,6 +229,14 @@ class ErrorHandler {
129229
}
130230
}
131231

232+
/**
233+
* Respond the request with JSON.
234+
*
235+
* @param {Request} request
236+
* @param {Toolkit} h
237+
*
238+
* @returns {Response}
239+
*/
132240
sendJson (request, h) {
133241
const error = this.composeError(request)
134242
const json = this.resolveJson(error)
@@ -139,17 +247,39 @@ class ErrorHandler {
139247
.code(error.statusCode)
140248
}
141249

250+
/**
251+
* JSON.stringify the error object and
252+
* prettify the stackstrace.
253+
*
254+
* @param {Object} data
255+
*
256+
* @returns {String}
257+
*/
142258
resolveJson (data) {
143259
return JSON.stringify({
144260
...data,
145261
stacktrace: data.stacktrace.split('\n').map(line => line.trim())
146262
})
147263
}
148264

265+
/**
266+
* Determine whether the user wants to
267+
* render their own template.
268+
*
269+
* @returns {Boolean}
270+
*/
149271
hasTemplate () {
150272
return !!this.template
151273
}
152274

275+
/**
276+
* Render the defined user template.
277+
*
278+
* @param {Request} request
279+
* @param {Toolkit} h
280+
*
281+
* @returns {Response}
282+
*/
153283
renderTemplate (request, h) {
154284
const error = this.composeError(request)
155285

@@ -158,6 +288,14 @@ class ErrorHandler {
158288
.code(error.statusCode)
159289
}
160290

291+
/**
292+
* Respond the request with HTML.
293+
*
294+
* @param {Request} request
295+
* @param {Toolkit} h
296+
*
297+
* @returns {Response}
298+
*/
161299
async sendHtml (request, h) {
162300
const youch = this.createYouch(request)
163301
const statusCode = request.response.output.statusCode

0 commit comments

Comments
 (0)