UNPKG

@rapharacing/error-handler

Version:

Error Handler

53 lines (44 loc) 1.26 kB
const de = require("./locales/de/common.json"); const en = require("./locales/en/common.json"); const es = require("./locales/es/common.json"); const fr = require("./locales/fr/common.json"); const ja = require("./locales/ja/common.json"); const ko = require("./locales/ko/common.json"); const zh = require("./locales/zh/common.json"); const locales = { de, en, es, fr, ja, ko, zh }; /** * Returns a translated error message * * @method concat * @param {String} error - Error String * @param {String} locale - Locale String * @param {Boolean} cs - Customer Service * @returns {String} Translated error message */ const getError = (error, locale, cs) => { let json; if (error === undefined || typeof error !== "string") { throw new Error("No Error supplied"); } if (locale === undefined || typeof error !== "string") { throw new Error("No Locale supplied"); } if (locale.includes("-")) { json = locales[locale.split("-")[0]]; } else { json = locales[locale]; } // If cs is passed get the cs key else just get the normal error const err = cs ? `CS_${error}` : error; // If undefined then return the english version return json[err] ? json[err] : locales.en[err]; }; export default getError;