Skip to content

Commit

Permalink
Return same error responses as production for Edge Functions (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
laktek authored Oct 4, 2023
2 parents cae3249 + a48d875 commit a6290db
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
48 changes: 44 additions & 4 deletions internal/functions/serve/templates/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FunctionConfig> = (() => {
try {
const functionsConfig = JSON.parse(FUNCTIONS_CONFIG_STRING);
Expand Down Expand Up @@ -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,
);
}
}, {
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a6290db

Please sign in to comment.