54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
"use strict";
|
|
/**
|
|
* HTTP error helpers for consistent API responses.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.HttpError = void 0;
|
|
exports.httpStatusForError = httpStatusForError;
|
|
class HttpError extends Error {
|
|
constructor(statusCode, message, code, expose = true) {
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
this.code = code;
|
|
this.expose = expose;
|
|
this.name = 'HttpError';
|
|
}
|
|
}
|
|
exports.HttpError = HttpError;
|
|
/** Maps thrown errors to an HTTP status code. */
|
|
function httpStatusForError(error) {
|
|
if (error instanceof HttpError)
|
|
return error.statusCode;
|
|
const code = error?.code;
|
|
if (code === 'EMAIL_NOT_VERIFIED')
|
|
return 403;
|
|
if (code === 'WITHDRAWAL_2FA_REQUIRED')
|
|
return 403;
|
|
if (code === 'WITHDRAWALS_DISABLED' || code === 'WITHDRAWAL_NETWORK_DISABLED')
|
|
return 403;
|
|
if (code === 'INSUFFICIENT_BALANCE' ||
|
|
code === 'HOT_WALLET_INSUFFICIENT_LIQUIDITY' ||
|
|
code === 'TRANSFER_MIN_REMAINING' ||
|
|
code === 'TRANSFER_LEDGER_ONLY') {
|
|
return 400;
|
|
}
|
|
if (code === 'DAILY_WITHDRAWAL_LIMIT_EXCEEDED')
|
|
return 403;
|
|
if (code === 'TOKEN_REVOKED')
|
|
return 401;
|
|
if (code === 'ACCOUNT_LOCKED')
|
|
return 429;
|
|
if (code === 'CAPTCHA_REQUIRED' || code === 'CAPTCHA_INVALID')
|
|
return 400;
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
if (/Permission denied|Forbidden|not allowed/i.test(msg))
|
|
return 403;
|
|
if (/not found/i.test(msg))
|
|
return 404;
|
|
if (/virus scan|failed virus scan/i.test(msg))
|
|
return 422;
|
|
if (/Unauthorized|Invalid|required|Captcha|2FA|Password|Email|Account/i.test(msg))
|
|
return 400;
|
|
return 500;
|
|
}
|
|
//# sourceMappingURL=httpError.js.map
|