Skip to content

Commit 80d487b

Browse files
committed
Errors
1 parent bf3dbbd commit 80d487b

File tree

4 files changed

+87
-24
lines changed

4 files changed

+87
-24
lines changed

package-lock.json

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "worker-functions",
3-
"version": "0.5.0",
3+
"version": "0.5.7",
44
"description": "Full-stack Typesafety with Cloudflare Workers 👷",
55
"main": "generated-client/index.ts",
66
"scripts": {
@@ -35,6 +35,7 @@
3535
"typescript": "^4.9.5"
3636
},
3737
"dependencies": {
38-
"cross-fetch": "^3.1.5"
38+
"cross-fetch": "^3.1.5",
39+
"devalue": "^4.3.0"
3940
}
4041
}

templates/client.ts

+44-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
11
import type { WorkerTypeFns } from "./type-gen";
2-
import { fetch } from "cross-fetch";
3-
2+
import { fetch as crossFetch } from "cross-fetch";
3+
import * as devalue from "devalue";
44
const ourFetch = async (
5+
fetchImpl: typeof crossFetch,
56
endpoint: string,
67
functionName: string,
78
...params: any
89
) => {
9-
const resp = await fetch(`${endpoint}/${encodeURIComponent(functionName)}`, {
10-
body: JSON.stringify(params),
11-
method: "POST",
12-
headers: {
13-
"Content-Type": "application/json",
14-
},
15-
});
10+
const resp = await fetchImpl(
11+
`${endpoint}/${encodeURIComponent(functionName)}`,
12+
{
13+
body: devalue.stringify(params, {
14+
Error: (e) => e instanceof Error && e.message
15+
}),
16+
method: "POST",
17+
headers: {
18+
"Content-Type": "application/json"
19+
}
20+
}
21+
);
1622

17-
if (resp.ok) return resp.json();
23+
if (resp.ok) {
24+
const errorHeader = devalue.parse(
25+
resp.headers.get("X-Worker-Functions-Error"),
26+
{
27+
Error: (m) => new Error(m)
28+
}
29+
);
30+
if (errorHeader !== null) {
31+
throw errorHeader;
32+
}
33+
return devalue.parse(await resp.text(), {
34+
Error: (m) => new Error(m)
35+
});
36+
}
37+
throw new Error("Failed to fetch");
1838
};
1939

20-
export function WorkerClient(endpoint: string) {
40+
export function WorkerClient(
41+
endpoint: string,
42+
options: { fetch?: typeof crossFetch } = {
43+
fetch: crossFetch
44+
}
45+
) {
2146
return new Proxy(
2247
{},
2348
{
2449
get: (_, fn) => {
2550
// @ts-ignore
26-
return (...params) => ourFetch(endpoint, fn, ...params);
27-
},
51+
return (...params) =>
52+
ourFetch(
53+
options.fetch ?? crossFetch,
54+
endpoint,
55+
fn as string,
56+
...params
57+
);
58+
}
2859
}
2960
) as WorkerTypeFns;
3061
}

templates/worker.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as devalue from "devalue";
2+
13
export default {
24
async fetch(
35
request: Request,
@@ -28,13 +30,31 @@ export default {
2830
const functionName = decodeURIComponent(url.pathname.split("/")[1]);
2931
let input: any[] = [];
3032
try {
31-
input = (await request.json()) as unknown[];
33+
input = devalue.parse(await request.text(), {
34+
Error: (m) => new Error(m)
35+
}) as unknown[];
3236
} catch {}
33-
return Response.json(await functions[functionName].bind(env)(...input), {
34-
headers: {
35-
"Access-Control-Allow-Origin": "*",
36-
"Access-Control-Allow-Method": "POST"
37+
let data: any = null;
38+
let error: any = null;
39+
try {
40+
data = await functions[functionName].bind(env)(...input);
41+
} catch (e) {
42+
error = e;
43+
}
44+
return new Response(
45+
devalue.stringify(data, {
46+
Error: (e) => e instanceof Error && e.message
47+
}),
48+
{
49+
headers: {
50+
"X-Worker-Functions-Error": devalue.stringify(error, {
51+
Error: (e) => e instanceof Error && e.message
52+
}),
53+
"Content-Type": "application/json",
54+
"Access-Control-Allow-Origin": "*",
55+
"Access-Control-Allow-Method": "POST"
56+
}
3757
}
38-
});
58+
);
3959
}
4060
};

0 commit comments

Comments
 (0)