-
Notifications
You must be signed in to change notification settings - Fork 1
/
Response.js
222 lines (188 loc) · 4.96 KB
/
Response.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
/**
* ## res
* Middleware for express response; adds error handling.
* The original express respones is also passed as `res.originalResponse`
*/
var log = require('rf-log');
var db
module.exports = function (res, database) {
db = database;
// makes 'this' usable within child functions
var self = this;
self.originalResponse = res;
/** ### res.send()
*
* default response function; adds error handling
*
* Example: Simple
* ```js
* res.send(err, data);
* ```
*
* Example: respond from db with error handling
* ```js
* db.user.groups // send all groups back to client
* .find({})
* .exec(res.send);
* ```
*
* Example: using the callback function
* ```js
* createDocs()
*
* function createDocs(){
* createDoc(function (err, doc){
* res.send(err, docs, processDocs);
* })
* }
*
* function processDocs(){
* console.log(docs)
* res.send(err, docs);
* }
* // linearize asynchron code with the successFunction. instead of:
* //
* // execute function A
* // then execute function B
* // afterwards execute function C
* // afterwards execute function D
* //
* // the code is linearized:
* //
* // execute function A
* //
* // function A
* // then execute function B
* //
* // function B
* // then execute function C
* //
* // function C
* // then execute function D
* //
* // advantages: better readabilty, automatic error names for each function
* ```
*/
self.send = function (err, docs, callback) {
if (!err && callback && typeof (callback) === 'function') {
callback(docs);
} else {
send(err, docs, res);
}
};
/** ### res.errors
* Send back error with specific error code
* ```js
* res.error("statusRed")
* // status 500; standard error; if error isn't handeled
* ```
*/
self.error = function (err) {
handleError(err, function (result) {
send('Server Error: ' + result, null, res, 500);
log.error('Server Error: ' + result);
});
};
/** ```js
* res.errorBadRequest("Missing id")
* // status 400; missing or wrong parameters
* ```
*/
self.errorBadRequest = function (err) {
send('Bad request: ' + err, null, res, 400);
};
/** ```js
* res.errorAuthorizationRequired()
* // status 401; not autorized for route
* ```
*/
self.errorAuthorizationRequired = function (msg) {
send(`Authorization required! ${msg || ''}`, null, res, 401);
};
/** ```js
* res.errorAccessDenied("You need be admin")
* // status 403; request not allowed for user
* ```
*/
self.errorAccessDenied = function (err) {
send('Access denied: ' + err, null, res, 403);
};
/** ```js
* res.errorNotFound("No user found")
* // status 404; not found or not available
* ```
*/
self.errorNotFound = function (err) {
send('Not found: ' + err, null, res, 404);
};
/** ```js
* res.errorAlreadyExists("User exists")
* // status 409
* ```
*/
self.errorAlreadyExists = function (err) {
send('Already Exists: ' + err, null, res, 409);
};
/** ```js
* res.errorNoLongerExists("User is gone")
* // status 410; tried to save an entry wich was removed?
* ```
*/
self.errorNoLongerExists = function (err) {
send(err, null, res, 410);
};
};
/* -------------- helper functions ----------------- */
function send (err, docs, res, status) {
// handle requests
if (err) {
status = status || 500;
handleError(err, function (result) {
res
.status(status)
.send(result)
.end();
});
} else { // success; last step
status = status || 200;
res
.status(status)
.json(docs)
.end();
}
}
function handleError (error, callback) {
var endPoint, stack, type;
if (typeof error === 'object') {
if (error.endPoint) endPoint = error.endPoint;
// MongoDB Unique Error
if (error.code === 11000) {
error = error.errmsg;
} else if (error.message && error.stack) {
var errStack = error.stack.split('\n');
type = errStack[0];
stack = errStack.slice(1);
error = 'Error: ' + error.message;
} else {
error = JSON.stringify(error);
}
}
if (!endPoint) return callback(error);
db.statistic.endPoints
.findOneAndUpdate(
{ name: endPoint.name, method: endPoint.method },
{ $push: {
events: {
stack: stack,
eventType: type,
date: new Date(),
data: endPoint.data
}
} },
{ upsert: true, new: true },
function (err) {
if (err) console.log('Error while saving error in statistic', err);
callback(error);
}
);
}