import type { ErrorCode } from "@batch-cooking/shared";

/**
 * Typed error carrying both the HTTP status it should map to and the
 * business {@link ErrorCode} that identifies *why* it happened.
 *
 * Route handlers throw this (or let it bubble from a service call) instead
 * of manually setting a status/body — {@link ErrorHandlerService} is the
 * single place that turns it into an actual HTTP response, so every error
 * path in an app built with these tools is shaped consistently.
 */
export class HttpError extends Error {
  /** HTTP status code to respond with (e.g. 401, 404, 409). */
  public readonly status: number;
  /** Machine-readable error code, shared with the client — see {@link ErrorCode}. */
  public readonly code: ErrorCode;

  /**
   * @param status - HTTP status code to respond with.
   * @param code - Business error code identifying the failure (shared with the client).
   * @param message - Developer-facing description (English). Logged/used for
   *   debugging only; end-user-facing text is derived client-side from `code`.
   */
  public constructor(status: number, code: ErrorCode, message: string) {
    super(message);
    this.name = "HttpError";
    this.status = status;
    this.code = code;
  }
}
