centralized-error-codes
Version:
Centralized error codes for SDK
75 lines (74 loc) • 3.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorCode = void 0;
var fs = require("fs");
var path = require("path");
var ErrorCode = /** @class */ (function () {
function ErrorCode() {
}
ErrorCode.initialize = function () {
var _this = this;
try {
var modulePath_1 = path.dirname(require.resolve('central-error-codes/package.json'));
var filePath = path.join(modulePath_1, 'dist/typescript/error-code.json');
var data = fs.readFileSync(filePath, 'utf-8');
this.errors = JSON.parse(data);
// Load additional error files dynamically
Object.keys(this.errors).forEach(function (key) {
if (typeof _this.errors[key] === 'string') {
_this.errors[key] = _this.loadErrorFile(_this.errors[key], modulePath_1);
}
});
}
catch (error) {
console.error("Error loading error-code.json:", error);
}
};
ErrorCode.loadErrorFile = function (filePath, modulePath) {
try {
var absolutePath = path.join(modulePath, 'dist/typescript/errors', filePath);
var data = fs.readFileSync(absolutePath, 'utf-8');
return JSON.parse(data);
}
catch (error) {
console.error("Error reading file at ".concat(filePath, ":"), error);
return null;
}
};
/** Returns all error codes (without grouping under GENERIC_ERROR or LLM_GW_ERROR) */
ErrorCode.getError = function () {
var _a, _b;
return Object.assign({}, (_a = this.errors) === null || _a === void 0 ? void 0 : _a.GENERIC_ERROR, (_b = this.errors) === null || _b === void 0 ? void 0 : _b.LLM_GW_ERROR);
};
/** Returns only GENERIC_ERROR codes */
ErrorCode.getGenericError = function () {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.GENERIC_ERROR) || {};
};
/** Returns only LLM_GW_ERROR codes */
ErrorCode.getLLMGatewayError = function () {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.LLM_GW_ERROR) || {};
};
ErrorCode.getCMSGatewayError = function () {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.CMS_GW_ERROR) || {};
};
ErrorCode.getRecommndationError = function () {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.RECOMMENDATION_TOOL_ERROR) || {};
};
ErrorCode.getReviewToolsError = function () {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.REVIEW_TOOL_ERROR) || {};
};
ErrorCode.getErrorByCategory = function (category) {
var _a;
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a[category]) || {};
};
ErrorCode.errors = {};
return ErrorCode;
}());
exports.ErrorCode = ErrorCode;
// Initialize error codes on import
ErrorCode.initialize();
;