Skip to content

Commit 617f607

Browse files
committed
*: ⚠️ error handling and creation changes
1 parent 79298ff commit 617f607

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

test/v2.errors.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ await Deno.test("Error Propagation: Bad Job Config returns ProdiaUserError", {
3434
err instanceof ProdiaUserError,
3535
"Error should be a ProdiaUserError",
3636
);
37-
assertStringIncludes(err.message, "jsonschema validation failed");
38-
assertStringIncludes(err.message, "steps/maximum");
37+
assertStringIncludes(err.message, "steps");
3938
assertStringIncludes(err.message, "maximum: got 1,000");
39+
assert(
40+
err.message.match(
41+
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/,
42+
),
43+
"Error message should contain a job id",
44+
);
4045
}
4146
});
4247

@@ -49,11 +54,14 @@ await Deno.test("Error Propagation: No Job Type returns ProdiaCapacityError", {
4954
});
5055

5156
try {
52-
await client.job({
53-
config: {
54-
prompt: "puppies in a cloud, 4k",
57+
await client.job(
58+
// @ts-ignore
59+
{
60+
config: {
61+
prompt: "puppies in a cloud, 4k",
62+
},
5563
},
56-
});
64+
);
5765

5866
throw new Error("Job should not succeed");
5967
} catch (err) {

v2/index.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,21 @@ type JsonValue = JsonPrimitive | JsonObject | JsonArray;
2424

2525
/* job and job configuration */
2626

27-
export type ProdiaJob = Record<string, JsonValue>;
27+
export type ProdiaJob = {
28+
type: string;
29+
config?: Record<string, JsonValue>;
30+
};
31+
32+
type ProdiaJobComplete = ProdiaJob & {
33+
id: string;
34+
created_at: string;
35+
updated_at: string;
36+
expires_at: string;
37+
metrics: {
38+
elapsed: number;
39+
ips?: number;
40+
};
41+
};
2842

2943
export type ProdiaJobOptions = {
3044
accept?:
@@ -41,22 +55,20 @@ const defaultJobOptions: ProdiaJobOptions = {
4155
accept: undefined,
4256
};
4357

44-
export type ProdiaJobResponse = {
45-
job: ProdiaJob;
46-
47-
// currently these are the only output field for all job types.
48-
// they will return the raw bytes for that output.
49-
arrayBuffer: () => Promise<ArrayBuffer>;
50-
uint8Array: () => Promise<Uint8Array>;
51-
};
52-
5358
/* client & client configuration*/
5459

5560
export type Prodia = {
5661
job: (
5762
params: ProdiaJob,
5863
options?: Partial<ProdiaJobOptions>,
59-
) => Promise<ProdiaJobResponse>;
64+
) => Promise<{
65+
job: ProdiaJobComplete;
66+
67+
// currently these are the only output field for all job types.
68+
// they will return the raw bytes for that output.
69+
arrayBuffer: () => Promise<ArrayBuffer>;
70+
uint8Array: () => Promise<Uint8Array>;
71+
}>;
6072
};
6173

6274
export type CreateProdiaOptions = {
@@ -139,7 +151,7 @@ export const createProdia = ({
139151
body: formData,
140152
});
141153

142-
// We bail from the loop if we get a 2xx response to avoid sleeping unnecessarily.
154+
// we bail from the loop if we get a 2xx response to avoid sleeping unnecessarily.
143155
if (response.status >= 200 && response.status < 300) {
144156
break;
145157
}
@@ -151,6 +163,7 @@ export const createProdia = ({
151163
}
152164

153165
const retryAfter = Number(response.headers.get("Retry-After")) || 1;
166+
154167
await new Promise((resolve) =>
155168
setTimeout(resolve, retryAfter * 1000)
156169
);
@@ -169,23 +182,11 @@ export const createProdia = ({
169182
);
170183
}
171184

172-
if (response.headers.get("Content-Type") === "application/json") {
173-
const body = await response.json();
174-
175-
if ("error" in body && typeof body.error === "string") {
176-
throw new ProdiaUserError(body.error);
177-
}
178-
179-
const lastStateHistory = body.state.history.slice(-1)[0];
180-
181-
if (lastStateHistory && "message" in lastStateHistory) {
182-
throw new ProdiaUserError(lastStateHistory.message);
183-
}
184-
185-
throw new Error("Job Failed: Bad Content-Type: application/json");
186-
}
187-
188-
if (response.status < 200 || response.status > 299) {
185+
if (
186+
!response.headers.get("Content-Type")?.startsWith(
187+
"multipart/form-data",
188+
)
189+
) {
189190
throw new ProdiaBadResponseError(
190191
`${response.status} ${await response.text()}`,
191192
);
@@ -197,10 +198,12 @@ export const createProdia = ({
197198
new TextDecoder().decode(
198199
await (body.get("job") as Blob).arrayBuffer(),
199200
),
200-
) as ProdiaJob;
201+
) as ProdiaJobComplete;
201202

202-
if ("error" in job && typeof job.error === "string") {
203-
throw new ProdiaUserError(job.error);
203+
if (response.status >= 400 && response.status < 500) {
204+
if ("error" in job && typeof job.error === "string") {
205+
throw new ProdiaUserError(job.error + " " + `(id: ${job.id})`);
206+
}
204207
}
205208

206209
return {

0 commit comments

Comments
 (0)