-
Notifications
You must be signed in to change notification settings - Fork 13
/
JsonResponseHandler.js
108 lines (93 loc) · 2.87 KB
/
JsonResponseHandler.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
/**
* JsonResponseHandler.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 Handler = require('./Handler');
var formatter = require('../exception/formatter');
var util = require('util');
var _ = require('lodash');
/**
* Catches an exception and converts it to a JSON
* response. Additionally can also return exception
* frames for consumption by an API.
*/
function JsonResponseHandler(onlyForAjaxOrJsonRequests, returnFrames, sendResponse) {
JsonResponseHandler.super_.call(this);
/**
* 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);
/**
* @var {boolean}
* @protected
*/
this.__returnFrames = returnFrames ? Boolean(returnFrames) : false;
/**
* If this is true handler will process this error if and only
* if request is ajax or request accepts json.
*
* @var {boolean}
* @protected
*/
this.__onlyForAjaxOrJsonRequests = onlyForAjaxOrJsonRequests ? Boolean(onlyForAjaxOrJsonRequests) : false;
}
util.inherits(JsonResponseHandler, Handler);
/**
* Check, if possible, that this execution was triggered by an AJAX request.
*
* @return bool
* @protected
*/
JsonResponseHandler.prototype.__isAjaxRequest = function () {
if (this.__request) {
return (
!_.isEmpty(this.__request.headers['x-requested-with'])
&& (this.__request.headers['x-requested-with']).toLowerCase() == 'xmlhttprequest');
}
};
/**
* Check, whether request content type is JSON.
*
* @return bool
* @protected
*/
JsonResponseHandler.prototype.__wantsJson = function () {
if (this.__request) {
return (
!_.isEmpty(this.__request.headers['accept'])
&& (this.__request.headers['accept']).toLowerCase() == 'application/json');
}
};
/**
* @return int
*/
JsonResponseHandler.prototype.handle = function (next) {
if (this.__onlyForAjaxOrJsonRequests && !(this.__isAjaxRequest() || this.__wantsJson())) {
next();
} else {
var output = {
'error': formatter.formatExceptionAsDataArray(
this.__inspector,
this.__returnFrames
)
};
if (this.__response && this.__sendResponse) {
if (!this.__response.headersSent) {
this.__response.writeHead('Content-Type: application/json');
}
this.__response.end(JSON.stringify(output));
next(null, Handler.QUIT);
} else {
next(JSON.stringify(output));
}
}
};
module.exports = JsonResponseHandler;