{"version":3,"file":"HttpError.mjs","sources":["../../src/http/HttpError.ts"],"sourcesContent":["/**\n * Specialized error class for HTTP request failures\n *\n * `HttpError` is a custom error class that is thrown when an HTTP request fails\n * and receives an error response from a remote server. Unlike the standard\n * Error class, it carries detailed HTTP-specific information (method, path,\n * status code, headers) that enables more sophisticated error handling and\n * debugging in HTTP communication scenarios.\n *\n * This class is particularly useful for:\n *\n * - API client libraries that need to provide detailed error information\n * - Applications that require different handling based on HTTP status codes\n * - Logging and monitoring systems that need structured error data\n * - Debugging HTTP communication issues\n *\n * @author Jeongho Nam - https://github.com/samchon\n */\nexport class HttpError extends Error {\n  /** The HTTP method that was used for the failed request */\n  public readonly method: \"GET\" | \"DELETE\" | \"POST\" | \"PUT\" | \"PATCH\" | \"HEAD\";\n\n  /** The path or URL that was requested */\n  public readonly path: string;\n\n  /** The HTTP status code returned by the server */\n  public readonly status: number;\n\n  /** The HTTP response headers returned by the server as key-value pairs */\n  public readonly headers: Record<string, string | string[]>;\n\n  /** @internal */\n  private body_: any = NOT_YET;\n\n  /**\n   * Creates a new HttpError instance\n   *\n   * Initializes an HttpError with comprehensive information about the failed\n   * HTTP request. This constructor calls the parent Error constructor to set\n   * the basic error message, and additionally stores HTTP-specific details as\n   * readonly properties for later access.\n   *\n   * @example\n   *   ```typescript\n   *   const error = new HttpError(\n   *     'POST',\n   *     '/api/login',\n   *     401,\n   *     { 'content-type': 'application/json', 'www-authenticate': 'Bearer' },\n   *     '{\"error\": \"Invalid credentials\", \"code\": \"AUTH_FAILED\"}'\n   *   );\n   *   console.log(error.status); // 401\n   *   console.log(error.method); // \"POST\"\n   *   ```;\n   *\n   * @param method The HTTP method that was used for the request (GET, POST,\n   *   PUT, DELETE, PATCH, HEAD)\n   * @param path The path or URL that was requested (e.g., '/api/users' or\n   *   'https://api.example.com/users')\n   * @param status The HTTP status code returned by the server (e.g., 404, 500,\n   *   401)\n   * @param headers The HTTP response headers returned by the server as\n   *   key-value pairs\n   * @param message The error message from the server (typically the response\n   *   body text)\n   */\n  public constructor(\n    method: \"GET\" | \"DELETE\" | \"POST\" | \"PUT\" | \"PATCH\" | \"HEAD\",\n    path: string,\n    status: number,\n    headers: Record<string, string | string[]>,\n    message: string,\n  ) {\n    super(message);\n    this.method = method;\n    this.path = path;\n    this.status = status;\n    this.headers = headers;\n\n    // INHERITANCE POLYFILL\n    const proto: HttpError = new.target.prototype;\n    if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);\n    else (this as any).__proto__ = proto;\n  }\n\n  /**\n   * Serializes the HttpError instance to a JSON-compatible object\n   *\n   * This method serves two primary purposes:\n   *\n   * 1. **Automatic serialization**: When `JSON.stringify()` is called on an\n   *    HttpError, this method is automatically invoked to provide a clean JSON\n   *    representation\n   * 2. **Structured error data**: When the server response body contains JSON,\n   *    this method parses it and provides structured access to error details\n   *\n   * The method implements lazy parsing for the response message:\n   *\n   * - JSON parsing is attempted only on the first call to avoid unnecessary\n   *   processing\n   * - Successful parsing results are cached for subsequent calls\n   * - If JSON parsing fails (e.g., for HTML error pages or plain text), the\n   *   original string is preserved and returned as-is\n   *\n   * @template T The expected type of the response body (defaults to any for\n   *   flexibility)\n   * @returns A structured object containing all HTTP error information with the\n   *   message field containing either the parsed JSON object or the original\n   *   string\n   */\n  public toJSON<T>(): HttpError.IProps<T> {\n    if (this.body_ === NOT_YET)\n      try {\n        this.body_ = JSON.parse(this.message);\n      } catch {\n        this.body_ = this.message;\n      }\n    return {\n      method: this.method,\n      path: this.path,\n      status: this.status,\n      headers: this.headers,\n      message: this.body_,\n    };\n  }\n}\nexport namespace HttpError {\n  /**\n   * Return type definition for the {@link HttpError.toJSON} method\n   *\n   * This interface provides a structured representation of all HTTP error\n   * information in a format suitable for serialization, logging, debugging, and\n   * API responses. It ensures consistency across different parts of an\n   * application when handling HTTP errors.\n   *\n   * The generic type parameter `T` allows for type-safe access to the parsed\n   * response body when the structure is known at compile time.\n   *\n   * @example\n   *   ```typescript\n   *   // Using with known error response structure\n   *   interface ApiErrorResponse {\n   *     code: string;\n   *     message: string;\n   *     details?: Record<string, any>;\n   *   }\n   *\n   *   const errorProps: HttpError.IProps<ApiErrorResponse> = httpError.toJSON<ApiErrorResponse>();\n   *   console.log(errorProps.message.code); // Type-safe access to error code\n   *   ```;\n   *\n   * @template T The type of the message field (parsed response body)\n   */\n  export interface IProps<T> {\n    /** The HTTP method used for the request */\n    method: \"GET\" | \"DELETE\" | \"POST\" | \"PUT\" | \"PATCH\" | \"HEAD\";\n\n    /** The path or URL that was requested */\n    path: string;\n\n    /** The HTTP status code returned by the server */\n    status: number;\n\n    /**\n     * The response headers as key-value pairs (values can be string or string\n     * array)\n     */\n    headers: Record<string, string | string[]>;\n\n    /** The response body (either parsed JSON object or original string) */\n    message: T;\n  }\n}\n\n/** @internal */\nconst NOT_YET = {} as any;\n"],"names":["HttpError","Error","constructor","method","path","status","headers","message","super","this","body_","NOT_YET","proto","prototype","Object","setPrototypeOf","__proto__","toJSON","JSON","parse"],"mappings":"AAkBM,MAAOA,kBAAkBC;IAgD7B,WAAAC,CACEC,QACAC,MACAC,QACAC,SACAC;QAEAC,MAAMD;QAzCAE,KAAAC,QAAaC;QA0CnBF,KAAKN,SAASA;QACdM,KAAKL,OAAOA;QACZK,KAAKJ,SAASA;QACdI,KAAKH,UAAUA;QAGf,MAAMM,mBAA8BC;QACpC,IAAIC,OAAOC,gBAAgBD,OAAOC,eAAeN,MAAMG,aACjDH,KAAaO,YAAYJ;AACjC;IA2BO,MAAAK;QACL,IAAIR,KAAKC,UAAUC,SACjB;YACEF,KAAKC,QAAQQ,KAAKC,MAAMV,KAAKF;AAC/B,UAAE;YACAE,KAAKC,QAAQD,KAAKF;AACpB;QACF,OAAO;YACLJ,QAAQM,KAAKN;YACbC,MAAMK,KAAKL;YACXC,QAAQI,KAAKJ;YACbC,SAASG,KAAKH;YACdC,SAASE,KAAKC;;AAElB;;;AAmDF,MAAMC,UAAU,CAAA;;"}