UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

29 lines (28 loc) 1.01 kB
import SecureJSON from "secure-json-parse"; import { z } from "zod"; import { ApiCallError } from "../../util/api/ApiCallError.js"; export const stabilityErrorDataSchema = z.object({ message: z.string(), }); export class StabilityError extends ApiCallError { constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) { super({ message, statusCode, requestBodyValues, url }); Object.defineProperty(this, "data", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.data = data; } } export const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => { const responseBody = await response.text(); const parsedError = stabilityErrorDataSchema.parse(SecureJSON.parse(responseBody)); return new StabilityError({ url, requestBodyValues, statusCode: response.status, data: parsedError, }); };