near-protocol-rewards
Version:
A transparent, metric-based rewards system for NEAR projects
49 lines (48 loc) • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toJSONErrorContext = exports.toErrorContext = exports.formatError = void 0;
const errors_1 = require("../types/errors");
/**
* Formats any error into a consistent structure
* for logging and error handling
*/
function formatError(error) {
if (error instanceof Error) {
return {
code: errors_1.ErrorCode.UNKNOWN_ERROR,
message: error.message,
context: {
name: error.name,
stack: error.stack,
},
};
}
if (typeof error === "string") {
return {
code: errors_1.ErrorCode.UNKNOWN_ERROR,
message: error,
};
}
return {
code: errors_1.ErrorCode.UNKNOWN_ERROR,
message: "Unknown error occurred",
context: { error: JSON.stringify(error) },
};
}
exports.formatError = formatError;
function toErrorContext(error) {
return { error: formatError(error) };
}
exports.toErrorContext = toErrorContext;
function toJSONErrorContext(error) {
const formatted = formatError(error);
const jsonError = {
code: formatted.code,
message: formatted.message,
};
if (formatted.context) {
jsonError.context = formatted.context;
}
return { error: jsonError };
}
exports.toJSONErrorContext = toJSONErrorContext;