MCPcopy Create free account
hub / github.com/fontsource/fontsource / toErrorResponse

Function toErrorResponse

api/worker/src/utils/errors.ts:23–54  ·  view source on GitHub ↗
(
	c: Context,
	error: unknown,
)

Source from the content-addressed store, hash-verified

21 * consistent.
22 */
23export const toErrorResponse = async (
24 c: Context,
25 error: unknown,
26): Promise<Response> => {
27 // Chanfana validation errors: HTTPException with a pre-built response body.
28 if (error instanceof HTTPException && error.res) {
29 try {
30 const { errors } = (await error.res.clone().json()) as {
31 errors?: Array<{ message: string; path?: string[] | null }>;
32 };
33 if (Array.isArray(errors) && errors.length > 0) {
34 const parts = errors.map(({ message, path }) =>
35 Array.isArray(path) && path.length > 0
36 ? `${path.join('.')}: ${message}`
37 : message,
38 );
39 return c.json(
40 { status: error.status, error: `Bad Request. ${parts.join('; ')}.` },
41 error.status as 400,
42 );
43 }
44 } catch {
45 // fall through
46 }
47 }
48
49 const status = error instanceof HTTPException ? error.status : 500;
50 const message =
51 error instanceof Error ? error.message : 'Internal Server Error.';
52
53 return c.json({ status, error: message }, status as 400 | 404 | 500 | 502);
54};

Callers 1

app.tsFile · 0.90

Calls 2

jsonMethod · 0.65
cloneMethod · 0.65

Tested by

no test coverage detected