Improve error handling
This commit is contained in:
+22
-24
@@ -12,11 +12,12 @@ import { cors } from "@elysiajs/cors";
|
||||
import { Elysia, NotFoundError, status } from "elysia";
|
||||
import {
|
||||
PrintfulClient,
|
||||
PrintfulError,
|
||||
ProductSyncer,
|
||||
WebflowClient,
|
||||
WebflowError,
|
||||
Printful,
|
||||
Webflow,
|
||||
FetchError,
|
||||
} from "@blade-and-brawn/commerce";
|
||||
import zipcodesUs from "zipcodes-us";
|
||||
import z from "zod";
|
||||
@@ -66,14 +67,18 @@ export const app = new Elysia()
|
||||
)
|
||||
|
||||
.error({
|
||||
FetchError,
|
||||
PrintfulError,
|
||||
WebflowError,
|
||||
})
|
||||
|
||||
.onError(async ({ code, error }) => {
|
||||
.onError(({ code, error }) => {
|
||||
switch (code) {
|
||||
case "FetchError":
|
||||
console.error(error.message, await error.parse());
|
||||
return error;
|
||||
case "PrintfulError":
|
||||
case "WebflowError":
|
||||
console.error(error.message, error.status, error.payload);
|
||||
return status(502, { error: error.message });
|
||||
default:
|
||||
console.error(error);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -130,36 +135,35 @@ export const app = new Elysia()
|
||||
.post("/", async ({ }) => {
|
||||
const syncState = await productSyncer.getState();
|
||||
if (syncState.isSyncing)
|
||||
return status(409, { error: "Sync already in progress" });
|
||||
throw status(409, "Sync already in progress");
|
||||
|
||||
await productSyncer.sync();
|
||||
return { ok: true };
|
||||
})
|
||||
// Per-product sync
|
||||
.post("/:printfulProductId", async ({ params }) => {
|
||||
const syncState = await productSyncer.getState();
|
||||
if (syncState.isSyncing)
|
||||
return status(409, { error: "Sync already in progress" });
|
||||
throw status(409, "Sync already in progress");
|
||||
|
||||
await productSyncer.sync(params.printfulProductId);
|
||||
return { ok: true };
|
||||
}, { params: z.object({ printfulProductId: z.number() }) }),
|
||||
}, { params: z.object({ printfulProductId: z.coerce.number() }) }),
|
||||
)
|
||||
// TODO: add schema validation
|
||||
.get("/:printfulProductId", async ({ params }) => {
|
||||
const printfulProductId = +params.printfulProductId;
|
||||
const printfulProduct =
|
||||
await printful.Products.get(printfulProductId);
|
||||
if (!printfulProduct) return new NotFoundError();
|
||||
await printful.Products.get(params.printfulProductId);
|
||||
if (!printfulProduct) throw new NotFoundError("Missing printful product");
|
||||
|
||||
const webflowProductId =
|
||||
printfulProduct.sync_product.external_id.split("-")[0];
|
||||
if (!webflowProductId) return new NotFoundError();
|
||||
if (!webflowProductId) throw new NotFoundError("Missing webflow product ID");
|
||||
|
||||
const webflowProduct =
|
||||
await webflow.Products.get(webflowProductId);
|
||||
|
||||
return { printfulProduct, webflowProduct };
|
||||
}, {
|
||||
params: z.object({ printfulProductId: z.coerce.number() })
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -194,14 +198,10 @@ export const app = new Elysia()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
})
|
||||
.post("/webhook/webflow", async ({ request, body, set }) => {
|
||||
if (!webflow.Util.verifyWebflowSignature(request, body)) {
|
||||
set.status = 400;
|
||||
return "Invalid signature";
|
||||
}
|
||||
.post("/webhook/webflow", async ({ request, body }) => {
|
||||
if (!webflow.Util.verifyWebflowSignature(request, body))
|
||||
throw status(400, "Invalid signature");
|
||||
|
||||
const payload = body as Webflow.Webhook.EventPayload;
|
||||
|
||||
@@ -237,8 +237,6 @@ export const app = new Elysia()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
Reference in New Issue
Block a user