-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathutils.js
More file actions
63 lines (55 loc) · 1.68 KB
/
utils.js
File metadata and controls
63 lines (55 loc) · 1.68 KB
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
const axios = require("axios");
const https = require("https");
const version = "0.5.1";
const invalidArgumentsType = "invalid_arguments";
function getApiManager(apiUrl, token) {
return axios.create({
baseURL: apiUrl,
headers: {
"User-Agent": `serverless-scaleway-functions/${version}`,
"X-Auth-Token": token,
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
}
/**
* Custom Error class, to print an error message, and pass the Response if applicable
*/
class CustomError extends Error {
constructor(message, response) {
super(message);
this.response = response;
}
}
/**
* Display the right error message, check if error has a response and data attribute
* to properly display either the global error, or the component-level error (function/container)
* @param {Error} err - Error thrown
*/
function manageError(err) {
err.response = err.response || {};
if (!err.response || !err.response.data) {
throw new Error(err);
}
if (err.response.data.message) {
let message = err.response.data.message;
// In case the error is an InvalidArgumentsError, provide some extra information
if (err.response.data.type === invalidArgumentsType) {
for (const details of err.response.data.details) {
const argumentName = details.argument_name;
const helpMessage = details.help_message;
message += `\n${argumentName}: ${helpMessage}`;
}
}
throw new CustomError(message, err.response);
} else if (err.response.data.error_message) {
throw new CustomError(err.response.data.error_message, err.response);
}
}
module.exports = {
getApiManager,
manageError,
CustomError,
};