2018-05-04 21:00:19 +00:00
|
|
|
// apiError(key) will be called by all controllers / api tests
|
|
|
|
|
// it includes the api- and commonErrors, since common-ops are used in the api too
|
|
|
|
|
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
2019-10-15 15:17:44 +00:00
|
|
|
import common from '../../common';
|
|
|
|
|
|
|
|
|
|
const commonErrors = common.errorMessages.common;
|
|
|
|
|
const apiErrors = common.errorMessages.api;
|
2018-05-04 21:00:19 +00:00
|
|
|
|
2024-03-11 14:59:57 +00:00
|
|
|
function apiError (msgKey, vars = {}) {
|
2018-05-04 21:00:19 +00:00
|
|
|
let message = apiErrors[msgKey];
|
|
|
|
|
if (!message) message = commonErrors[msgKey];
|
|
|
|
|
if (!message) throw new Error(`Error processing the API message "${msgKey}".`);
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const clonedVars = vars ? _.clone(vars) : {};
|
2018-05-04 21:00:19 +00:00
|
|
|
|
|
|
|
|
// TODO cache the result of template() ? More memory usage, faster output
|
|
|
|
|
return _.template(message)(clonedVars);
|
|
|
|
|
}
|
2024-03-11 14:59:57 +00:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
apiError,
|
|
|
|
|
};
|