UNPKG

@paykit-sdk/core

Version:

The Payment Toolkit for Typescript

33 lines (31 loc) 983 B
// src/tools/classify-error.ts var STATUS_MAP = { 401: "unauthorized", 403: "forbidden", 404: "not_found", 429: "rate_limit", 500: "internal_server_error", 502: "bad_gateway", 503: "service_unavailable", 504: "gateway_timeout" }; var CONNECTION_CODES = /ECONNREFUSED|ECONNRESET|ENOTFOUND|FETCH FAILED/; var TIMEOUT_CODES = /ETIMEDOUT|ECONNABORTED|TIMEOUT/; function classifyError(err, status) { if (status && STATUS_MAP[status]) return STATUS_MAP[status]; if (err instanceof Error) { const statusPrefix = /^(\d{3}):/.exec(err.message); if (statusPrefix) { const mapped = STATUS_MAP[Number(statusPrefix[1])]; if (mapped) return mapped; } const causeCode = String( err.cause?.code ?? "" ); const haystack = `${err.message} ${causeCode}`.toUpperCase(); if (CONNECTION_CODES.test(haystack)) return "connection"; if (TIMEOUT_CODES.test(haystack)) return "timeout"; } return "unknown"; } export { classifyError };