Centralized errors and added their translations. Didn't test
This commit is contained in:
		@@ -2,23 +2,23 @@ import config from '../../config.json' with {type: "json"};
 | 
			
		||||
 | 
			
		||||
const debug = (text) => {
 | 
			
		||||
    if (config.debug) console.debug(`[D] [${Date()}]: ${text}`);
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const info = (text) => {
 | 
			
		||||
    console.log(`[I] [${Date()}]: ${text}`);
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const error = (text) => {
 | 
			
		||||
    console.error(`[E] [${Date()}]: ${text}`);
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const warn = (text) => {
 | 
			
		||||
    console.warn(`[W] [${Date()}]: ${text}`);
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const unknownError = (text) => {
 | 
			
		||||
    error(text);
 | 
			
		||||
    return "Unknown server error. Please, report to the developer";
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default { debug, info, error, warn, unknownError };
 | 
			
		||||
@@ -1,28 +1,16 @@
 | 
			
		||||
import statuses from "./status.js";
 | 
			
		||||
import customError from "../response/customError.js";
 | 
			
		||||
import responseCodes from "../response/responseCodes.js";
 | 
			
		||||
 | 
			
		||||
const errorHandler = (e, obj) => {
 | 
			
		||||
    switch (e.code) {
 | 
			
		||||
        case '23505':
 | 
			
		||||
            throw {
 | 
			
		||||
                status: statuses.duplicate,
 | 
			
		||||
                message: `Such ${obj} already exists`
 | 
			
		||||
            };
 | 
			
		||||
            throw new customError(`Duplicate ${obj}`, responseCodes.responses[obs].duplicate)
 | 
			
		||||
        case '22007':
 | 
			
		||||
            throw {
 | 
			
		||||
                status: statuses.invalid_syntax,
 | 
			
		||||
                message: `Invalid syntax in ${obj}`
 | 
			
		||||
            };
 | 
			
		||||
            throw new customError(`Invalid syntax ${obj}`, responseCodes.responses.general.invalid_syntax)
 | 
			
		||||
        case '22001':
 | 
			
		||||
            throw {
 | 
			
		||||
                status: statuses.invalid_syntax,
 | 
			
		||||
                message: `Value too long (${obj})`
 | 
			
		||||
            };
 | 
			
		||||
            throw new customError(`Value too long ${obj}`, responseCodes.responses[obj].too_long)
 | 
			
		||||
        default:
 | 
			
		||||
            throw {
 | 
			
		||||
                status: statuses.unknown,
 | 
			
		||||
                message: `Unknown error. Please, report to the developer`,
 | 
			
		||||
                original: e
 | 
			
		||||
            };
 | 
			
		||||
            throw new customError(`Unknown error ${obj}`, responseCodes.responses.general.unknown)
 | 
			
		||||
    };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
const statuses = {
 | 
			
		||||
    ok: "ok",
 | 
			
		||||
    duplicate: "duplicate",
 | 
			
		||||
    not_found: "not found",
 | 
			
		||||
    invalid_syntax: "invalid syntax",
 | 
			
		||||
    unknown: "unknown"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default statuses;
 | 
			
		||||
							
								
								
									
										7
									
								
								src/utils/translate.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/utils/translate.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
const translate = (language, code) => {
 | 
			
		||||
    if (!language) language = "en-US"
 | 
			
		||||
    if (!code) code = "unknown"
 | 
			
		||||
    return JSON.parse(fs.readFileSync(`../../messages/${language}/msgs.json`).toString())[code]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default translate
 | 
			
		||||
		Reference in New Issue
	
	Block a user