erlc-api 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -17
- package/examples/error-handling.js +159 -0
- package/package.json +1 -1
- package/src/errors/ErlcError.js +34 -0
- package/src/errors/errorCodes.js +218 -0
- package/src/functions/server/getBans.js +17 -18
- package/src/functions/server/getCommandLogs.js +17 -18
- package/src/functions/server/getJoinLogs.js +17 -18
- package/src/functions/server/getKillLogs.js +17 -18
- package/src/functions/server/getModcallLogs.js +17 -18
- package/src/functions/server/getPlayers.js +17 -18
- package/src/functions/server/getQueue.js +17 -18
- package/src/functions/server/getServer.js +46 -19
- package/src/functions/server/getStaff.js +17 -18
- package/src/functions/server/getVehicles.js +17 -18
- package/src/functions/server/runCommand.js +20 -23
- package/src/types/index.d.ts +30 -1
- package/src/utils/errorHandler.js +158 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves join/leave logs from a server
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/joinlogs`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves kill logs from a server
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/killlogs`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves moderator call logs from a server
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/modcalls`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves current players from a server
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/players`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves server queue information
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/queue`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,29 +1,49 @@
|
|
|
1
1
|
const { BASEURL, Vanity } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
module.exports = (serverToken) => {
|
|
4
5
|
return new Promise(async (resolve, reject) => {
|
|
6
|
+
// Input validation
|
|
7
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
8
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
9
|
+
}
|
|
10
|
+
|
|
5
11
|
try {
|
|
6
12
|
const fetch = await import("node-fetch");
|
|
7
13
|
const { config } = await import("../../erlc.js");
|
|
8
14
|
|
|
15
|
+
// Check if global token is configured
|
|
16
|
+
if (!config?.globalToken) {
|
|
17
|
+
return reject(
|
|
18
|
+
new Error(
|
|
19
|
+
"Global token not configured. Please initialize the client first."
|
|
20
|
+
)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
9
24
|
const res = await fetch.default(`${BASEURL}/server`, {
|
|
10
25
|
headers: {
|
|
11
|
-
|
|
26
|
+
Authorization: config?.globalToken,
|
|
12
27
|
"Server-Key": serverToken,
|
|
13
28
|
},
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const data = await res.json().catch((err) => {
|
|
17
|
-
return reject(err);
|
|
29
|
+
timeout: 10000, // 10 second timeout
|
|
18
30
|
});
|
|
19
31
|
|
|
20
32
|
if (!res.ok) {
|
|
21
|
-
|
|
33
|
+
const errorData = await res
|
|
34
|
+
.json()
|
|
35
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
36
|
+
const error = await processError(res, errorData);
|
|
37
|
+
return reject(error);
|
|
22
38
|
}
|
|
23
39
|
|
|
40
|
+
const data = await res.json();
|
|
41
|
+
|
|
24
42
|
const getUsername = async (userId) => {
|
|
25
43
|
try {
|
|
26
|
-
const response = await fetch.default(
|
|
44
|
+
const response = await fetch.default(
|
|
45
|
+
`https://users.roblox.com/v1/users/${userId}`
|
|
46
|
+
);
|
|
27
47
|
const userData = await response.json();
|
|
28
48
|
if (!response.ok) {
|
|
29
49
|
console.warn(`Warning: Could not fetch username for ID: ${userId}`);
|
|
@@ -31,7 +51,10 @@ module.exports = (serverToken) => {
|
|
|
31
51
|
}
|
|
32
52
|
return userData.name;
|
|
33
53
|
} catch (error) {
|
|
34
|
-
console.warn(
|
|
54
|
+
console.warn(
|
|
55
|
+
`Warning: Error fetching username for ID: ${userId}`,
|
|
56
|
+
error
|
|
57
|
+
);
|
|
35
58
|
return `User:${userId}`; // Fallback format
|
|
36
59
|
}
|
|
37
60
|
};
|
|
@@ -39,12 +62,13 @@ module.exports = (serverToken) => {
|
|
|
39
62
|
try {
|
|
40
63
|
// Get owner username
|
|
41
64
|
const ownerUsername = await getUsername(data.OwnerId);
|
|
42
|
-
|
|
65
|
+
|
|
43
66
|
// Get co-owner usernames (handle empty array case)
|
|
44
|
-
const coOwnerUsernames =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
67
|
+
const coOwnerUsernames =
|
|
68
|
+
data.CoOwnerIds && data.CoOwnerIds.length > 0
|
|
69
|
+
? await Promise.all(data.CoOwnerIds.map(getUsername))
|
|
70
|
+
: [];
|
|
71
|
+
|
|
48
72
|
// Create vanity URL
|
|
49
73
|
const vanityURL = `${Vanity}${data.JoinKey}`;
|
|
50
74
|
|
|
@@ -53,7 +77,7 @@ module.exports = (serverToken) => {
|
|
|
53
77
|
...data,
|
|
54
78
|
OwnerUsername: ownerUsername,
|
|
55
79
|
CoOwnerUsernames: coOwnerUsernames,
|
|
56
|
-
VanityURL: vanityURL
|
|
80
|
+
VanityURL: vanityURL,
|
|
57
81
|
};
|
|
58
82
|
|
|
59
83
|
// Remove the original ID properties as they're now converted to usernames
|
|
@@ -63,16 +87,19 @@ module.exports = (serverToken) => {
|
|
|
63
87
|
resolve(enhancedData);
|
|
64
88
|
} catch (error) {
|
|
65
89
|
// If username fetching fails, still return the original data with IDs
|
|
66
|
-
console.warn(
|
|
90
|
+
console.warn(
|
|
91
|
+
"Warning: Could not fetch usernames, returning data with IDs:",
|
|
92
|
+
error
|
|
93
|
+
);
|
|
67
94
|
const fallbackData = {
|
|
68
95
|
...data,
|
|
69
|
-
VanityURL: `${Vanity}${data.JoinKey}
|
|
96
|
+
VanityURL: `${Vanity}${data.JoinKey}`,
|
|
70
97
|
};
|
|
71
98
|
resolve(fallbackData);
|
|
72
99
|
}
|
|
73
|
-
|
|
74
100
|
} catch (error) {
|
|
75
|
-
|
|
101
|
+
const processedError = await processError(error);
|
|
102
|
+
reject(processedError);
|
|
76
103
|
}
|
|
77
104
|
});
|
|
78
|
-
};
|
|
105
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves server staff information
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/staff`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(data || { CoOwners: [], Admins: {}, Mods: {} });
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { BASEURL } = require("../../constants.js");
|
|
2
|
+
const { processError } = require("../../utils/errorHandler.js");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Retrieves server vehicles information
|
|
@@ -8,8 +9,8 @@ const { BASEURL } = require("../../constants.js");
|
|
|
8
9
|
module.exports = (serverToken) => {
|
|
9
10
|
return new Promise(async (resolve, reject) => {
|
|
10
11
|
// Input validation
|
|
11
|
-
if (!serverToken || typeof serverToken !==
|
|
12
|
-
return reject(new Error(
|
|
12
|
+
if (!serverToken || typeof serverToken !== "string") {
|
|
13
|
+
return reject(new Error("Server token is required and must be a string"));
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
try {
|
|
@@ -18,37 +19,35 @@ module.exports = (serverToken) => {
|
|
|
18
19
|
|
|
19
20
|
// Check if global token is configured
|
|
20
21
|
if (!config?.globalToken) {
|
|
21
|
-
|
|
22
|
+
const error = await processError(
|
|
23
|
+
new Error(
|
|
24
|
+
"Global token not configured. Please initialize the client first."
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
return reject(error);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const res = await fetch.default(`${BASEURL}/server/vehicles`, {
|
|
25
31
|
headers: {
|
|
26
|
-
|
|
32
|
+
Authorization: config.globalToken,
|
|
27
33
|
"Server-Key": serverToken,
|
|
28
34
|
},
|
|
29
35
|
timeout: 10000, // 10 second timeout
|
|
30
36
|
});
|
|
31
37
|
|
|
32
38
|
if (!res.ok) {
|
|
33
|
-
const errorData = await res
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
error
|
|
39
|
+
const errorData = await res
|
|
40
|
+
.json()
|
|
41
|
+
.catch(() => ({ error: "Unknown API error" }));
|
|
42
|
+
const error = await processError(res, errorData);
|
|
37
43
|
return reject(error);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const data = await res.json();
|
|
41
47
|
resolve(Array.isArray(data) ? data : []);
|
|
42
|
-
|
|
43
48
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
reject(new Error('Network error: Unable to connect to ER:LC API'));
|
|
47
|
-
} else if (error.name === 'AbortError') {
|
|
48
|
-
reject(new Error('Request timeout: API took too long to respond'));
|
|
49
|
-
} else {
|
|
50
|
-
reject(error);
|
|
51
|
-
}
|
|
49
|
+
const processedError = await processError(error);
|
|
50
|
+
reject(processedError);
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
|
-
};
|
|
53
|
+
};
|