Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2: add user error handling #16

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ type JsonValue = JsonPrimitive | JsonObject | JsonArray;
export type ProdiaJob = Record<string, JsonValue>;

export type ProdiaJobOptions = {
accept:
| "image/png"
accept?:
| "application/json"
| "image/jpeg"
| "image/png"
| "image/webp"
| "multipart/form-data"
| "video/mp4";
inputs?: (File | Blob | ArrayBuffer)[];
};

const defaultJobOptions: ProdiaJobOptions = {
accept: "image/jpeg",
accept: undefined,
};

export type ProdiaJobResponse = {
arrayBuffer: () => Promise<ArrayBuffer>; // we only support direct image response now
job: ProdiaJob;

// Currently only one output field is expected for all job types.
//This will return the raw bytes for that output.
arrayBuffer: () => Promise<ArrayBuffer>;
};

/* client & client configuration*/
Expand All @@ -62,6 +67,7 @@ export type CreateProdiaOptions = {

/* error types */

export class ProdiaUserError extends Error {}
export class ProdiaCapacityError extends Error {}
export class ProdiaBadResponseError extends Error {}

Expand All @@ -87,6 +93,9 @@ export const createProdia = ({

const formData = new FormData();

// TODO: The input content-type is assumed here, but it shouldn't be.
// Eventually we will support non-image inputs and we will need some way
// to specify the content-type of the input.
if (options.inputs !== undefined) {
for (const input of options.inputs) {
if (typeof File !== "undefined" && input instanceof File) {
Expand Down Expand Up @@ -122,11 +131,18 @@ export const createProdia = ({
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
Accept: options.accept,
Accept: ["multipart/form-data", options.accept].filter(
Boolean,
).join("; "),
},
body: formData,
});

// We bail from the loop if we get a 2xx response to avoid sleeping unnecessarily.
if (response.status >= 200 && response.status < 300) {
break;
}

if (response.status === 429) {
retries += 1;
} else if (response.status < 200 || response.status > 299) {
Expand All @@ -138,25 +154,47 @@ export const createProdia = ({
setTimeout(resolve, retryAfter * 1000)
);
} while (
response.status !== 400 &&
response.status !== 401 &&
response.status !== 403 &&
(response.status < 200 || response.status > 299) &&
errors <= maxErrors &&
retries <= maxRetries
);

if (response.status === 429) {
throw new ProdiaCapacityError(
"Unable to schedule job with current token",
"Unable to schedule the job with current token.",
);
}

const body = await response.formData();
const job = JSON.parse(
new TextDecoder().decode(
await (body.get("job") as Blob).arrayBuffer(),
),
) as ProdiaJob;
if ("error" in job && typeof job.error === "string") {
throw new ProdiaUserError(job.error);
}

if (response.status < 200 || response.status > 299) {
throw new ProdiaBadResponseError(
`${response.status} ${response.statusText}`,
);
}

const buffer = await new Promise<ArrayBuffer>((resolve, reject) => {
const output = body.get("output") as File;
const reader = new FileReader();
reader.readAsArrayBuffer(output);
reader.onload = () => resolve(reader.result as ArrayBuffer);
reader.onerror = () => reject(new Error("Failed to read output"));
});

return {
arrayBuffer: () => response.arrayBuffer(),
job: job,
arrayBuffer: () => Promise.resolve(buffer),
};
};

Expand Down
Loading