Skip to content

Commit

Permalink
*: error routing + test case
Browse files Browse the repository at this point in the history
  • Loading branch information
montyanderson committed Dec 18, 2024
1 parent 3164ec6 commit 0c7c3be
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
Binary file added test/landscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/man.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 43 additions & 2 deletions test/v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "jsr:@std/assert";
import { createProdia } from "../v2/index.ts";
import { assert, assertEquals } from "jsr:@std/assert";
import { createProdia, ProdiaUserError } from "../v2/index.ts";

const token = Deno.env.get("PRODIA_TOKEN");

Expand Down Expand Up @@ -32,3 +32,44 @@ await Deno.test("Example Job: JPEG Output", async () => {

assertEquals(isJpeg(image), true, "Image should be a JPEG");
});

await Deno.test("Example Job: Bad Face Swap", async () => {
const client = createProdia({
token,
});

try {
const job = await client.job(
{
type: "inference.faceswap.v1",
config: {},
},
{
inputs: [
new Blob([await Deno.readFile("./test/landscape.jpg")], {
type: "image/jpeg",
}),
new Blob([await Deno.readFile("./test/man.jpg")], {
type: "image/jpeg",
}),
],
},
);

const image = await job.arrayBuffer();

assertEquals(isJpeg(image), true, "Image should be a JPEG");

throw new Error("FaceSwap should fail");
} catch (error) {
assert(
error instanceof ProdiaUserError,
`Error should be a ProdiaUserError, got ${error}`,
);
assertEquals(
error.message,
"No face detected in the source image.",
"Error message should be 'No face detected in the source image.'",
);
}
});
26 changes: 20 additions & 6 deletions v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,42 @@ export const createProdia = ({
retries <= maxRetries
);

if (response.headers.get("Content-Type") === "application/json") {
const body = await response.json() as ProdiaJob;

if ("error" in body && typeof body.error === "string") {
throw new ProdiaUserError(body.error);
} else {
throw new ProdiaBadResponseError(
`${response.status} ${response.statusText}`,
);
}
}

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

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

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();
Expand Down

0 comments on commit 0c7c3be

Please sign in to comment.