Skip to content

Commit c2b0350

Browse files
authored
Merge pull request #45 from BrNi05/41-feature-expose-backend-error-msg-consts-so-ebs-can-use-them
Expose errors msgs for EBs
2 parents 7ce8925 + 1535cb1 commit c2b0350

5 files changed

Lines changed: 38 additions & 26 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discos",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"type": "module",

src/common.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Platform validation
2-
export const PLATFORM_ERR = 'DiscOS CRITICAL ERROR: Only Linux is supported. Startup aborted.';
2+
export const PLATFORM_ERR = 'DiscOS ERROR: Only Linux is supported. Startup aborted.';
33

44
// dotenv validation
55
export function ENV_MISSING_VAR(missingVars: string[]): string {
@@ -20,8 +20,8 @@ export const ENV_QUICK_VIEW_MAX_LENGTH = 'DiscOS ERROR: QUICK_VIEW_MAX_LENGTH mu
2020
// DB validation
2121
export const DB_ERR = 'DiscOS ERROR: Database file is invalid or missing.';
2222
export const DB_USER_ERR = 'DiscOS ERROR: users must be an object with numeric string keys (Discord UIDs) and string values (local users).';
23-
export const DB_USERS_EMPTY = 'DiscOS WARN: No users found in database.';
24-
export const DB_USERS_INVALID = 'User IDs must be numeric strings at least 17 characters long.';
23+
export const DB_USERS_EMPTY = 'DiscOS ERROR: No users found in database.';
24+
export const DB_USERS_INVALID = 'DiscOS ERROR: User IDs must be numeric strings at least 17 characters long.';
2525
export const DB_ADMIN_ERR = 'DiscOS ERROR: adminUsers must be an array of numeric strings with at least 17 characters.';
2626
export const DB_NO_ADMIN = 'DiscOS ERROR: There must be at least one admin user in the database.';
2727
export const DB_ADMIN_INVALID = 'DiscOS ERROR: adminUsers must be a non-empty array of numeric strings with at least 17 characters.';
@@ -329,15 +329,20 @@ export function ADMIN_GOODBYE(user: string): string {
329329
export const UNAME_PREFIX = 'discos';
330330

331331
// Backend
332-
export const SPAWN_ERR = 'DiscOS CRITICAL ERROR: cmdex is broken.';
333-
export const DIR_ERR = 'The provided path is an existing directory.';
334-
export const PERM_ERR = 'No permission to write to the file (or to enter the directories).';
335-
export const DOWNLOAD_ERR = 'Failed to download file from Discord CDN.';
336-
export const SET_PERM_ERR = 'Failed to set file permissions.';
332+
export const SPAWN_ERR = 'DiscOS ERROR: cmdex is broken.';
333+
export const EB_DIR_ERR = 'The provided path is an existing directory.';
334+
export const EB_PERM_ERR = 'No permission to write to the file (or to enter the directories).';
335+
export const EB_DOWNLOAD_ERR = 'Failed to download file from Discord CDN.';
336+
export const EB_SET_PERM_ERR = 'Failed to set file permissions.';
337337

338338
export const PING_FAILED = 'Ping failed: ';
339339
export const EXTERNAL_OK = 'External backend is online and responded to the health check ping.';
340340
export const EXTERNAL_NORESPONSE =
341341
'WARNING! The external backend did not respond to the health check ping. It may be offline or unreachable.';
342342

343343
export const NETWORK_ERR = 'DiscOS ERROR: Network error while trying to connect to the backend.';
344+
345+
// External backend specific
346+
export const EB_DB_ERR = 'DiscOS has encountered a critical error. Please contact the administrator.';
347+
export const EB_PAYLOAD_INVALID = 'DiscOS ERROR: Payload validation failed.';
348+
export const EB_GENERIC_ERR = 'DiscOS has encountered an unknown error. Failed to execute command.';

src/shared/consts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ export const PING_RESPONSE: string = 'DISCOS';
1010
// Database update dummy command
1111
// The suffix added to the backend URL when updating the database
1212
export const DB_UPDATE: string = 'dbupdate';
13+
14+
// External backend error messages
15+
export { EB_DB_ERR, EB_PAYLOAD_INVALID, EB_GENERIC_ERR, EB_DIR_ERR, EB_PERM_ERR, EB_DOWNLOAD_ERR, EB_SET_PERM_ERR } from '../common.js';

src/tools/backend.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ const execAsync = promisify(exec);
2929

3030
// Determines local user (based on Discord UID)
3131
async function localUser(user: string): Promise<string> {
32-
// Handle root user (for admos root <command>)
33-
if (user === ROOT_UID) {
34-
return 'root';
35-
}
32+
try {
33+
// Handle root user (for admos root <command>)
34+
if (user === ROOT_UID) {
35+
return 'root';
36+
}
3637

37-
const dbContent: string = await readFile(Config.databasePath, 'utf-8');
38-
const dbParsed = JSON.parse(dbContent) as {
39-
users: Record<string, string>;
40-
};
38+
const dbContent: string = await readFile(Config.databasePath, 'utf-8');
39+
const dbParsed = JSON.parse(dbContent) as {
40+
users: Record<string, string>;
41+
};
4142

42-
// Cannot be null, since only an allowedUser can send commands
43-
const serverUser: string = dbParsed.users[user];
43+
// Cannot be null, since only an allowedUser can send commands
44+
const serverUser: string = dbParsed.users[user];
4445

45-
return shellEscape([serverUser]);
46+
return shellEscape([serverUser]);
47+
} catch {
48+
return '.'; // Silently fail (cmd exec will return an error), but encountering such an error is basically impossible
49+
}
4650
}
4751

4852
// Axios response creation
@@ -98,21 +102,21 @@ async function fileWrite(url: string, path: string, payload: ICommandQueueItem):
98102
const isDirCmd = shellEscape([`[ -d ${escapedPath} ] && echo 1 || echo 0`]);
99103
let { stdout } = await execAsync(`sudo /usr/local/bin/cmdex ${serverUser} ${isDirCmd}`);
100104
if (stdout.trim() === '1') {
101-
return COMMON.DIR_ERR;
105+
return COMMON.EB_DIR_ERR;
102106
}
103107

104108
// Test if the user has permission to write to the file (while creating the neccessary directories)
105109
const testPermissionCmd = shellEscape([`mkdir -p "$(dirname ${escapedPath})" && echo > ${escapedPath} && rm -f ${escapedPath}`]);
106110
({ stdout } = await execAsync(`sudo /usr/local/bin/cmdex ${serverUser} ${testPermissionCmd}`));
107111
if (stdout.trim() !== '') {
108-
return COMMON.PERM_ERR;
112+
return COMMON.EB_PERM_ERR;
109113
}
110114

111115
// Download the file from Discord CDN
112116
const curlCmd = shellEscape([`curl -fsSL ${safeUrl} -o ${escapedPath}`]);
113117
({ stdout } = await execAsync(`sudo /usr/local/bin/cmdex ${serverUser} ${curlCmd}`));
114118
if (stdout.trim() !== '') {
115-
return COMMON.DOWNLOAD_ERR;
119+
return COMMON.EB_DOWNLOAD_ERR;
116120
}
117121

118122
// Set the file permissions to 660
@@ -123,7 +127,7 @@ async function fileWrite(url: string, path: string, payload: ICommandQueueItem):
123127
const permCmd = shellEscape([`cd ${userHomeDir} && chown ${serverUser}:${serverUser} ${escapedPath} && chmod 644 ${escapedPath}`]);
124128
({ stdout } = await execAsync(`sudo /usr/local/bin/cmdex root ${permCmd}`));
125129
if (stdout.trim() !== '') {
126-
return COMMON.SET_PERM_ERR;
130+
return COMMON.EB_SET_PERM_ERR;
127131
}
128132

129133
return WRITE_OP_SUCCESS;

0 commit comments

Comments
 (0)