Beginner
CDCloudflare Developers
•Created by marko.333 on 12/6/2024 in #workers-help
Errors Crossing RPC boundaries?
You can try:
In worker1:
async function callWorker2() {
const response = await fetch("https://worker2.example.com");
if (!response.ok) {
const errorDetails = await response.json();
if (errorDetails.type === "FooError") {
console.error("Caught a FooError:", errorDetails.bar);
} else {
console.error("Caught an error:", errorDetails.message);
}
}
}
In Worker2:
class FooError extends Error {
constructor(public bar: string = "testing") {
super(
FooError: ${bar});
this.name = "FooError";
}
}
async function handleRequest(request) {
try {
throw new FooError("This is a custom error");
} catch (error) {
if (error instanceof FooError) {
return new Response(JSON.stringify({
type: error.name,
message: error.message,
bar: error.bar
}), { status: 500 });
}
return new Response("Internal Server Error", { status: 500 });
}
}
4 replies
CDCloudflare Developers
•Created by marko.333 on 12/6/2024 in #workers-help
Errors Crossing RPC boundaries?
Instead of relying on implicit serialization, serialize your specialized exceptions explicitly into a structured format (e.g., JSON) before passing them across the boundary.
4 replies