· 6 years ago · Oct 17, 2019, 06:44 PM
1class FuckingError extends Error {
2 constructor(errorCode) {
3 super(errorCode + '');
4 this.status = errorCode;
5 this.name = 'FuckingError';
6 }
7}
8
9const notDefined = (thing) => (typeof thing === 'undefined');
10
11async function processRequest(jsonData, answerCallback) {
12 // @FIXME: Fucking workaround over a fucked-up architecture
13 //
14 // If the original dev had been writing this mess after the release of Node 4.0, they should be
15 // so fucking ashamed of themselves, if only for not reading the official node foundation code
16 // style recommendations. Bloody, fucking, hell!
17 const self = this;
18 const getSecretKey = (userId) => new Promise((resolve, reject) => {
19 /*
20 self.appsDB.getSecretKey((err, key) => {
21 if (!err) return resolve(key);
22
23 if (err instanceof DataBaseError) {
24 self.logDB.logIt('ERROR', `'${err.message}'`);
25 return reject(new FuckingError(402));
26 }
27
28 self.logDB.logIt('ERROR', `Unexpected error: Message: '${err.message}' StackTrace: ${err.stack}`);
29 reject(new FuckingError(500));
30 });
31 */
32 resolve(0);
33 });
34
35 const executeRequest = (params) => new Promise((resolve, reject) => {
36 self[params.methodName](params, (err, result) => {
37 if (err) return reject(err);
38 resolve(result);
39 });
40 });
41
42 try {
43 // Extracting used params
44 const userId = jsonData['id'];
45 const userKey = jsonData['key'];
46
47 if (notDefined(userKey)) throw new FuckingError(500);
48
49 // Retrieving userData
50 this.logDB.logIt('OK', 'Retrieving user...');
51
52 const secretKey = await getSecretKey(userId);
53 const userData = { id: userId, secretKey, userKey, jsonData };
54
55 // Checking user
56 this.logDB.logIt('OK', 'Verifying user..');
57
58 const requestMethod = jsonData['method'];
59 if (notDefined(requestMethod)) {
60 this.logDB.logIt('ERROR', 'Invalid request syntax: "method" field is missing');
61 throw new FuckingError(500);
62 }
63
64 const methodName = this.responseMethodsDict[requestMethod];
65 if (notDefined(methodName)) {
66 this.logDB.logIt('ERROR', `Invalid syntax of request: no such method = ${requestMethod}`);
67 throw new FuckingError(400);
68 }
69
70 // I have a feeling that use validation is missing here...
71 this.logDB.logIt('OK', `User keys for userId = ${userId} are equal`);
72 const params { secretKey, jsonData, methodName, userData };
73 const result = await executeRequest(params);
74
75 result.secretKey = crypto.getHash(crypto.computeTotp(secretKey), userId, `${methodName}server`);
76 return answerCallback(JSON.stringify(result, null, 1));
77 } catch (err) {
78 // Wrapping numeric errors for easy handling
79 if (typeof err === 'number') {
80 err = new FuckingError(err);
81 }
82
83 if (err instanceof FuckingError) {
84 console.log(err.status, 'error');
85 answerCallback(null, this.parent.getErrorResponse(err.status));
86 return;
87 }
88
89 // Handling unexpected errors
90 console.error('Encountered some weird error: ', err);
91 return;
92 }
93}