diff --git a/internal/functions/serve/templates/main.ts b/internal/functions/serve/templates/main.ts index 2331c5564..acd500e73 100644 --- a/internal/functions/serve/templates/main.ts +++ b/internal/functions/serve/templates/main.ts @@ -16,6 +16,25 @@ interface FunctionConfig { verifyJWT: boolean; } +enum WorkerErrors { + InvalidWorkerCreation = "InvalidWorkerCreation", + InvalidWorkerResponse = "InvalidWorkerResponse", +} + +function respondWith(payload: any, status: number, customHeaders = {}) { + const headers = { ...customHeaders }; + let body = null; + if (payload) { + headers["Content-Type"] = "application/json"; + body = JSON.stringify(payload); + } + const res = new Response(body, { + status, + headers, + }); + return res; +} + const functionsConfig: Record = (() => { try { const functionsConfig = JSON.parse(FUNCTIONS_CONFIG_STRING); @@ -135,10 +154,31 @@ serve(async (req: Request) => { return await worker.fetch(req, { signal }); } catch (e) { console.error(e); - const error = { msg: e.toString() }; - return new Response( - JSON.stringify(error), - { status: 500, headers: { "Content-Type": "application/json" } }, + if (e.name === WorkerErrors.InvalidWorkerCreation) { + return respondWith( + { + code: "BOOT_ERROR", + message: "Worker failed to boot (please check logs)", + }, + 503, + ); + } + if (e.name === WorkerErrors.InvalidWorkerResponse) { + return respondWith( + { + code: "WORKER_LIMIT", + message: + "Worker failed to respond due to an error or resource limit (please check logs)", + }, + 546, // custom error code + ); + } + return respondWith( + { + code: Status.InternalServerError, + message: "Request failed due to a server error", + }, + Status.InternalServerError, ); } }, { diff --git a/internal/utils/misc.go b/internal/utils/misc.go index 97003a4e5..27edba31f 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -33,7 +33,7 @@ const ( PgmetaImage = "supabase/postgres-meta:v0.68.0" StudioImage = "supabase/studio:20230921-d657f29" ImageProxyImage = "darthsim/imgproxy:v3.8.0" - EdgeRuntimeImage = "supabase/edge-runtime:v1.20.1" + EdgeRuntimeImage = "supabase/edge-runtime:v1.20.2" VectorImage = "timberio/vector:0.28.1-alpine" PgbouncerImage = "bitnami/pgbouncer:1.20.1-debian-11-r39" GotrueImage = "supabase/gotrue:v2.92.1"