/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import * as types from "../types/primitives.js";
import { Issue, Issue$inboundSchema } from "./issue.js";
import { VercelError } from "./vercelerror.js";

/**
 * The request did not match the expected schema
 */
export type HttpApiDecodeErrorData = {
  issues: Array<Issue>;
  message: string;
};

/**
 * The request did not match the expected schema
 */
export class HttpApiDecodeError extends VercelError {
  issues: Array<Issue>;

  /** The original data that was passed to this error instance. */
  data$: HttpApiDecodeErrorData;

  constructor(
    err: HttpApiDecodeErrorData,
    httpMeta: { response: Response; request: Request; body: string },
  ) {
    const message = err.message || `API error occurred: ${JSON.stringify(err)}`;
    super(message, httpMeta);
    this.data$ = err;
    this.issues = err.issues;

    this.name = "HttpApiDecodeError";
  }
}

/** @internal */
export const HttpApiDecodeError$inboundSchema: z.ZodType<
  HttpApiDecodeError,
  z.ZodTypeDef,
  unknown
> = z.object({
  issues: z.array(Issue$inboundSchema),
  message: types.string(),
  request$: z.instanceof(Request),
  response$: z.instanceof(Response),
  body$: z.string(),
})
  .transform((v) => {
    return new HttpApiDecodeError(v, {
      request: v.request$,
      response: v.response$,
      body: v.body$,
    });
  });
