-
Notifications
You must be signed in to change notification settings - Fork 13
/
PrettyPageHandler.js
363 lines (313 loc) · 10.5 KB
/
PrettyPageHandler.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* PrettyPageHandler.js
*
* @author: Harish Anchu <harishanchu@gmail.com>
* @copyright 2015, Harish Anchu. All rights reserved.
* @license Licensed under MIT (https://github.com/quorrajs/Ouch/blob/master/LICENSE)
*/
var path = require('path');
var fs = require('fs');
var formatter = require('../exception/formatter');
var _ = require('lodash');
var isEmpty = _.isEmpty;
var TemplateHelper = require('../util/TemplateHelper');
var url = require('url');
var os = require('os');
var inspect = require('util').inspect;
var Handler = require('./Handler');
var util = require('util');
/**
* Prettifies a javascript error stack.
*
* @param theme
* @param pageTitle
* @param editor
* @param sendResponse
* @param [additionalScripts]
* @class
*/
function PrettyPageHandler(theme, pageTitle, editor, sendResponse, additionalScripts) {
PrettyPageHandler.super_.call(this);
/**
* @var {String}
* @protected
*/
this.__pageTitle = pageTitle || "Ouch! There was an error.";
/**
* @var {String}
* @protected
*/
this.__theme = theme || "blue";
/**
* A string identifier for a known IDE/text editor, or a closure
* that resolves a string that can be used to open a given file
* in an editor. If the string contains the special substrings
* %file or %line, they will be replaced with the correct data.
*
* @example
* "txmt://open?url=%file&line=%line"
*
* @var {*} editor
* @protected
*/
this.__editor = editor;
/**
* Should Ouch push output directly to the client?
* If this is false, output will be passed to the callback
* provided to the handle method.
* @type {boolean}
* @protected
*/
this.__sendResponse = sendResponse === undefined ? true : Boolean(sendResponse);
/**
* An Array of urls that represent additional javascript resources to include in the rendered template.
*
* @type {Array}
* @protected
*/
this.__additionalScripts = additionalScripts || [];
/**
* A list of known editor strings
*
* @var Object
* @protected
*/
this.__editors = {
"sublime": "subl://open?url=file://%file&line=%line",
"textmate": "txmt://open?url=file://%file&line=%line",
"emacs": "emacs://open?url=file://%file&line=%line",
"macvim": "mvim://open/?url=file://%file&line=%line"
};
}
util.inherits(PrettyPageHandler, Handler);
/**
* Handles the error
*
* @param {function} next
*/
PrettyPageHandler.prototype.handle = function (next) {
var helper = new TemplateHelper();
var templateFile = this.__getResource("views/layout.ejs");
var cssFile = this.__getResource("css/ouch." + this.__theme + ".css");
var zeptoFile = this.__getResource("js/zepto.min.js");
var jsFile = this.__getResource("js/ouch.base.js");
var inspector = this.__inspector;
var frames = inspector.getFrames();
var code = inspector.getCode();
// List of variables that will be passed to the layout template.
var data = {
"pageTitle": this.__pageTitle,
"stylesheet": fs.readFileSync(cssFile),
"zepto": fs.readFileSync(zeptoFile),
"javascript": fs.readFileSync(jsFile),
"additionalScripts": this.__additionalScripts,
// Template paths:
"header": this.__getResource("views/header.ejs"),
"frameList": this.__getResource("views/frameList.ejs"),
"frameCode": this.__getResource("views/frameCode.ejs"),
"envDetails": this.__getResource("views/envDetails.ejs"),
"filename": templateFile,
"title": this.__pageTitle,
"name": inspector.getExceptionName(),
"message": inspector.getExceptionMessage(),
"code": code,
"plainException": formatter.formatExceptionPlain(inspector),
"frames": frames,
"hasFrames": frames.length,
"handler": this,
"handlers": this.__run ? this.__run.getHandlers() : [],
"isEmpty": isEmpty,
"inspect": inspect,
"tables": {
"Server/Request Data": this.__getServerAndRequestInfo(),
"params": this.__getRequestParams(),
"Cookies": this.__parseCookies(),
//"Session": [],
"Environment Variables": process.env
}
};
helper.setVariables(data);
var handlerResponse = helper.render(templateFile);
if (this.__response && this.__sendResponse) {
if (!this.__response.headersSent) {
this.__response.setHeader("Content-Type", "text/html");
this.__response.writeHead(code);
}
this.__response.end(handlerResponse);
next(null, Handler.QUIT);
} else {
next(handlerResponse);
}
};
/**
* Given a string file path, and an integer file line,
* executes the editor resolver and returns, if available,
* a string that may be used as the href property for that
* file reference.
*
* @throws ReferenceError If editor resolver does not return a string
* @param filePath
* @param line
* @returns {false|string}
*/
PrettyPageHandler.prototype.getEditorHref = function (filePath, line) {
if (!this.__editor/* || path.basename(filePath) === filePath*/) {
return false;
}
var editor = this.__editor;
if (_.isString(editor)) {
editor = this.__editors[editor];
}
if (_.isFunction(editor)) {
editor = editor.call(null, filePath, line);
}
// Check that the editor is a string, and replace the
// %line and %file placeholders:
if (!_.isString(editor)) {
throw new ReferenceError(
this.callee + " should always resolve to a string; got something else instead"
);
}
editor = editor.replace("%line", encodeURIComponent(line));
editor = editor.replace("%file", encodeURIComponent(filePath));
return editor;
};
/**
* Adds an editor resolver, identified by a string
* name, and that may be a string path, or a callable
* resolver. If the callable returns a string, it will
* be set as the file reference's href attribute.
*
* @example
* ouch.addEditor('macvim', "mvim://open?url=file://%file&line=%line")
* @example
* ouch.addEditor('remove-it', function(file, line) {
* ......
* return "http://stackoverflow.com";
* });
* @param {string} identifier
* @param {string|function} resolver
*/
PrettyPageHandler.prototype.addEditor = function (identifier, resolver) {
this.__editors[identifier] = resolver;
};
/**
* Set the editor to use to open referenced files, by a string
* identifier, or a callable that will be executed for every
* file reference, with a file and line argument, and should
* return a string.
*
* @example
* ouch.setEditor(function(file, line) { return "file:///"+ file; });
* @example
* ouch.setEditor('sublime');
*
* @throws ReferenceError If invalid argument identifier provided
* @param {string|function} editor
*/
PrettyPageHandler.prototype.setEditor = function (editor) {
if (!_.isFunction(editor) && !(this.__editors[editor])) {
throw new ReferenceError(
"Unknown editor identifier: editor. Known editors:".
Object.keys(this.__editors).join(", ")
);
}
this.__editor = editor;
};
/**
* @throws Error If resource cannot be found.
*
* @param {string} resource
* @returns string
* @protected
*/
PrettyPageHandler.prototype.__getResource = function (resource) {
var fullPath = path.join(__dirname, '../resources/', resource);
if (fs.existsSync(fullPath)) {
return fullPath;
}
// If we got this far, nothing was found.
throw new Error("Could not find resource " + resource + " in resource path.");
};
/**
* Parses cookies from request headers.
*
* @returns {Object}
*/
PrettyPageHandler.prototype.__parseCookies = function () {
var list = {};
if (this.__request !== null) {
var rc = this.__request.headers.cookie;
rc && rc.split(';').forEach(function (cookie) {
var parts = cookie.split('=');
list[parts.shift().trim()] = decodeURI(parts.join('='));
});
}
return list;
};
/**
* Returns query string data and request body if it exists from request object.
*
* @returns {Object}
*/
PrettyPageHandler.prototype.__getRequestParams = function () {
var params = {};
if (this.__request !== null) {
var url_parts = url.parse(this.__request.url, true);
params.queryStringData = url_parts.query;
if (this.__request.body) {
params.requestBody = this.__request.body;
}
}
return params;
};
/**
* Returns Object containing general information about running node server and request.
*
* @returns {Object}
*/
PrettyPageHandler.prototype.__getServerAndRequestInfo = function () {
var info = {};
if (this.__request !== null) {
var req = this.__request;
info = {
REMOTE_ADDR: (req.headers['x-forwarded-for'] || '').split(',')[0] || req.connection.remoteAddress,
REMOTE_PORT: req.connection.remotePort,
SERVER_SOFTWARE: 'NodeJS ' + process.versions.node + " " + os.type(),
SERVER_PROTOCOL: this.__getRequestProtocol() + "/" + req.httpVersion,
//@todo: replace with host servername
//SERVER_NAME: os.hostname(),
//SERVER_PORT: this.__request.socket.localPort,
REQUEST_URI: this.__request.url,
REQUEST_METHOD: this.__request.method,
SCRIPT_FILE: require.main.filename,
PATH_INFO: url.parse(this.__request.url).pathname,
QUERY_STRING: url.parse(this.__request.url).query,
HTTP_HOST: req.headers.host,
HTTP_CONNECTION: req.headers.connection,
HTTP_CACHE_CONTROL: req.headers['cache-control'],
HTTP_ACCEPT: req.headers.accept,
HTTP_USER_AGENT: req.headers['user-agent'],
HTTP_DNT: req.headers.dnt,
HTTP_ACCEPT_ENCODING: req.headers['accept-encoding'],
HTTP_ACCEPT_LANGUAGE: req.headers['accept-language'],
HTTP_COOKIE: req.headers.cookie
}
}
return info;
};
/**
* Returns request connection protocol.
*
* @returns {String}
*/
PrettyPageHandler.prototype.__getRequestProtocol = function () {
var proto = this.__request.connection.encrypted
? 'https'
: 'http';
// Note: X-Forwarded-Proto is normally only ever a
// single value, but this is to be safe.
proto = this.__request.headers['X-Forwarded-Proto'] || proto;
return proto.split(/\s*,\s*/)[0].toUpperCase();
};
module.exports = PrettyPageHandler;