-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
104 lines (86 loc) · 2.53 KB
/
utils.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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
// Contributed by @jpjpjp
//
const assert = require("assert");
const utils = {};
/*
* Transmits an error with the Webex format
* {
* "message": "Content type 'application/xml' not supported",
* "errors": [
* {
* "description": "Content type 'application/xml' not supported"
* }
* ],
* "trackingId": "NA_982ba0e9-a1a7-4eff-9be5-c6e5cdf94d73"
* }
*/
utils.sendError = function (res, statusCode, message, error) {
assert.ok((res), "no response specified");
assert.ok((statusCode), "no statusCode specified");
if (!message) {
_send(res, statusCode);
return;
}
if (!error) {
_send(res, statusCode, {
"message": message,
"errors": [
{
"description": message
}
],
"trackingId": res.locals.trackingId
});
return;
}
switch (typeof error) {
case "string":
_send(res, statusCode, {
"message": message,
"errors": [
{
"description": error
}
],
"trackingId": res.locals.trackingId
});
return;
case "array":
_send(res, statusCode, {
"message": message,
"errors": error,
"trackingId": res.locals.trackingId
});
return;
}
// Should not happen
debug("implementation issue 'Should not happen' in sendError");
sendError(res, 501, "[Emulator] implementation issue 'Should not happen' in sendError");
}
utils.sendSuccess = function (res, statusCode, body) {
assert.ok((res), "no response specified");
_send(res, statusCode, body);
}
// Send as new buffer to override express auto setting of Content-Type header
// We set this manually since Webex does not include a space, and utf-8 is uppercased
// "Content-type": "application/json;charset=UTF-8"
//
// See this as an enhancement to Express default: res.status(statusCode).send(body);
//
function _send(res, statusCode, body) {
if (!statusCode) {
res.status(200).send();
return;
}
if (!body) {
res.status(statusCode).send();
return;
}
res.setHeader("Content-Type", "application/json;charset=UTF-8");
res.status(statusCode).send(Buffer.from(JSON.stringify(body)));
}
module.exports = utils;