Centralized errors and added their translations. Didn't test
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/response/customError.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/response/customError.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
class customError extends Error {
 | 
			
		||||
    constructor(message, code) {
 | 
			
		||||
        super(message)
 | 
			
		||||
        if (Error.captureStackTrace) {
 | 
			
		||||
            Error.captureStackTrace(this, customError);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        this.code = code;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default customError;
 | 
			
		||||
							
								
								
									
										26
									
								
								src/response/errorHandler.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/response/errorHandler.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
import log from "../utils/log";
 | 
			
		||||
import customError from "./customError";
 | 
			
		||||
import responses from './responseCodes';
 | 
			
		||||
import translate from '../utils/translate.js';
 | 
			
		||||
 | 
			
		||||
const errorHandler = (err, req, res) => {
 | 
			
		||||
    log.error(err);
 | 
			
		||||
 | 
			
		||||
    let language = req.headers["accept-language"];
 | 
			
		||||
 | 
			
		||||
    if (err instanceof customError) {
 | 
			
		||||
        return res.status(responses.getHTTPCode(err.code)).send(translate(language, code));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const tryHandler = (func) => {
 | 
			
		||||
    return async (req, res, next) => {
 | 
			
		||||
        try {
 | 
			
		||||
            await func(req, res, next);
 | 
			
		||||
        } catch (err) {
 | 
			
		||||
            errorHandler(err, req, res);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default tryHandler;
 | 
			
		||||
							
								
								
									
										218
									
								
								src/response/responseCodes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										218
									
								
								src/response/responseCodes.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,218 @@
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
 | 
			
		||||
const responses = {
 | 
			
		||||
    authentication: {
 | 
			
		||||
        not_found: "authentication not found",
 | 
			
		||||
        invalid: "authentication invalid"
 | 
			
		||||
    },
 | 
			
		||||
    user: {
 | 
			
		||||
        duplicate: "user duplicate",
 | 
			
		||||
        invalid_syntax: "user invalid syntax",
 | 
			
		||||
        already_in_group: "user already in group"
 | 
			
		||||
    },
 | 
			
		||||
    usernames: {
 | 
			
		||||
        duplicate: "username taken",
 | 
			
		||||
        not_found: "username not found",
 | 
			
		||||
        required: "username required"
 | 
			
		||||
    },
 | 
			
		||||
    passwords: {
 | 
			
		||||
        required: "password required",
 | 
			
		||||
        invalid: "password invalid"
 | 
			
		||||
    },
 | 
			
		||||
    groups: {
 | 
			
		||||
        duplicate: "group name taken",
 | 
			
		||||
        name_not_found: "group name not found",
 | 
			
		||||
        id_not_found: "group id not found",
 | 
			
		||||
        not_an_owner: "group not an owner",
 | 
			
		||||
        not_a_member: "group not a member"
 | 
			
		||||
    },
 | 
			
		||||
    abstractproducts: {
 | 
			
		||||
        not_found: "abstract product not found",
 | 
			
		||||
        hash_without_file: "hash without file",
 | 
			
		||||
        file_without_hash: "file without hash",
 | 
			
		||||
        invalid_syntax: "abstract product invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    barcode: {
 | 
			
		||||
        not_found: "barcode not found",
 | 
			
		||||
        duplicate: "barcode duplicate",
 | 
			
		||||
        too_long: "barcode too long",
 | 
			
		||||
        invalid_syntax: "barcode invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    abstractproductname: {
 | 
			
		||||
        not_found: "abstract product name not found",
 | 
			
		||||
        duplicate: "abstract product name duplicate",
 | 
			
		||||
        too_long: "abstract product name too long",
 | 
			
		||||
        invalid_syntax: "abstract product name invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    netweight: {
 | 
			
		||||
        too_long: "net weight too long",
 | 
			
		||||
        invalid_syntax: "net weight invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    imagehash: {
 | 
			
		||||
        too_long: "image hash too long",
 | 
			
		||||
        invalid_syntax: "image hash invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    category: {
 | 
			
		||||
        too_long: "category too long",
 | 
			
		||||
        invalid_syntax: "category invalid syntax",
 | 
			
		||||
        not_found: "category not found"
 | 
			
		||||
    },
 | 
			
		||||
    unit: {
 | 
			
		||||
        too_long: "unit too long",
 | 
			
		||||
        invalid_syntax: "unit invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    products: {
 | 
			
		||||
        not_found: "product not found",
 | 
			
		||||
        invalid_syntax: "product invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    abstractproductid: {
 | 
			
		||||
        not_found: "abstract product id not found",
 | 
			
		||||
        invalid_syntax: "abstract product id invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    amount: {
 | 
			
		||||
        too_long: "amount too long",
 | 
			
		||||
        invalid_syntax: "amount invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    dateofproduction: {
 | 
			
		||||
        too_long: "date of production too long",
 | 
			
		||||
        invalid_syntax: "date of production invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    expirydate: {
 | 
			
		||||
        too_long: "expiry date too long",
 | 
			
		||||
        invalid_syntax: "expiry date invalid syntax"
 | 
			
		||||
    },
 | 
			
		||||
    categories: {
 | 
			
		||||
        duplicate: "categories duplicate",
 | 
			
		||||
        not_found: "categories not found"
 | 
			
		||||
    },
 | 
			
		||||
    general: {
 | 
			
		||||
        ok: "ok",
 | 
			
		||||
        invalid_syntax: "invalid syntax",
 | 
			
		||||
        png_only: "png only",
 | 
			
		||||
        too_long: "too long",
 | 
			
		||||
        unknown: "unknown"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getHTTPCode = (type) => {
 | 
			
		||||
    switch (code) {
 | 
			
		||||
        case responses.general.ok:
 | 
			
		||||
            return 200
 | 
			
		||||
        case responses.general.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.general.unknown:
 | 
			
		||||
            return 500
 | 
			
		||||
        case responses.general.png_only:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.general.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.authentication.invalid:
 | 
			
		||||
            return 403
 | 
			
		||||
        case responses.authentication.not_found:
 | 
			
		||||
            return 403
 | 
			
		||||
 | 
			
		||||
        case responses.user.already_in_group:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.user.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.user.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        case responses.usernames.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.usernames.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.usernames.required:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.passwords.required:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.passwords.invalid:
 | 
			
		||||
            return 403
 | 
			
		||||
 | 
			
		||||
        case responses.groups.id_not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.groups.name_not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.groups.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.groups.not_an_owner:
 | 
			
		||||
            return 403
 | 
			
		||||
        case responses.groups.not_a_member:
 | 
			
		||||
            return 403
 | 
			
		||||
 | 
			
		||||
        case responses.abstractproducts.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.abstractproducts.file_without_hash:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproducts.hash_without_file:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproducts.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.abstractproductid.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproductid.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
 | 
			
		||||
        case responses.abstractproductname.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.abstractproductname.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproductname.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.abstractproductname.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.abstractproducts.file_without_hash:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproducts.hash_without_file:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproducts.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.abstractproducts.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
 | 
			
		||||
        case responses.amount.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.amount.too_long:
 | 
			
		||||
            return 409
 | 
			
		||||
 | 
			
		||||
        case responses.barcode.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.barcode.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.barcode.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
        case responses.barcode.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.categories.duplicate:
 | 
			
		||||
            return 409
 | 
			
		||||
        case responses.categories.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
 | 
			
		||||
        case responses.category.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.category.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.category.not_found:
 | 
			
		||||
            return 404
 | 
			
		||||
 | 
			
		||||
        case responses.dateofproduction.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.dateofproduction.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
 | 
			
		||||
        case responses.expirydate.invalid_syntax:
 | 
			
		||||
            return 400
 | 
			
		||||
        case responses.expirydate.too_long:
 | 
			
		||||
            return 400
 | 
			
		||||
        default:
 | 
			
		||||
            return 500
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default { responses, getHTTPCode }
 | 
			
		||||
		Reference in New Issue
	
	Block a user