UNPKG

japanpost-api

Version:
133 lines 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationError = exports.GeneralAPIError = exports.NetworkError = exports.ServerError = exports.ClientError = exports.RateLimitError = exports.AuthorizationError = exports.AuthenticationError = exports.JapanPostAPIError = void 0; /** * Base class for Japan Post API related errors */ class JapanPostAPIError extends Error { constructor({ status, body, headers }, message) { const bodyText = body.replace(/\n/g, "").substring(0, 200); super(message || `JapanPostAPIError: (status: ${status}, body: ${bodyText} ...)`); this.status = status; this.headers = headers; this.body = body; try { this.error = JSON.parse(body); } catch (e) { this.error = null; } } /** * Create appropriate error class instance from error response */ static createFromResponse(options) { const { status } = options; if (status === 401) { return new AuthenticationError(options); } if (status === 403) { return new AuthorizationError(options); } if (status === 429) { return new RateLimitError(options); } if (status >= 400 && status < 500) { return new ClientError(options); } if (status >= 500) { return new ServerError(options); } return new GeneralAPIError(options); } } exports.JapanPostAPIError = JapanPostAPIError; /** * Authentication error (401 Unauthorized) */ class AuthenticationError extends JapanPostAPIError { constructor(options) { super(options, `Authentication failed. Token is invalid or expired. (status: ${options.status})`); this.name = "AuthenticationError"; } } exports.AuthenticationError = AuthenticationError; /** * Authorization error (403 Forbidden) */ class AuthorizationError extends JapanPostAPIError { constructor(options) { super(options, `Access denied. Please check your client permissions. (status: ${options.status})`); this.name = "AuthorizationError"; } } exports.AuthorizationError = AuthorizationError; /** * Rate limit error (429 Too Many Requests) */ class RateLimitError extends JapanPostAPIError { constructor(options) { super(options, `Rate limit exceeded. Please wait before retrying. (status: ${options.status})`); this.name = "RateLimitError"; // Get retry time from Retry-After header const retryAfterHeader = options.headers["retry-after"] || options.headers["Retry-After"]; if (retryAfterHeader) { this.retryAfter = parseInt(retryAfterHeader, 10); } } } exports.RateLimitError = RateLimitError; /** * Client error (4xx series) */ class ClientError extends JapanPostAPIError { constructor(options) { super(options, `Request error occurred. Please check your parameters. (status: ${options.status})`); this.name = "ClientError"; } } exports.ClientError = ClientError; /** * Server error (5xx series) */ class ServerError extends JapanPostAPIError { constructor(options) { super(options, `Server error occurred. Please wait and try again later. (status: ${options.status})`); this.name = "ServerError"; } } exports.ServerError = ServerError; /** * Network error */ class NetworkError extends Error { constructor(originalError) { super(`Network error occurred: ${originalError.message}`); this.name = "NetworkError"; this.originalError = originalError; } } exports.NetworkError = NetworkError; /** * General API error */ class GeneralAPIError extends JapanPostAPIError { constructor(options) { super(options); this.name = "GeneralAPIError"; } } exports.GeneralAPIError = GeneralAPIError; /** * Validation error */ class ValidationError extends Error { constructor(field, value, message) { super(`Validation error [${field}]: ${message} (value: ${value})`); this.name = "ValidationError"; this.field = field; this.value = value; } } exports.ValidationError = ValidationError; //# sourceMappingURL=JapanPostAPIError.js.map