If you want to get started quickly, click on the button below.
This creates a repository in your GitHub account and deploys the application to Cloudflare Workers.
exportdefault{asyncfetch(request,env,ctx){// Service configured to receive logsconstLOG_URL="https://log-service.example.com/";asyncfunctionpostLog(data){returnawaitfetch(LOG_URL,{method:"POST",body:data,});}letresponse;try{response=awaitfetch(request);if (!response.ok&&!response.redirected){constbody=awaitresponse.text();thrownewError("Bad response at origin. Status: "+response.status+" Body: "+// Ensure the string is small enough to be a headerbody.trim().substring(0,10),);}}catch (err){// Without ctx.waitUntil(), your fetch() to Cloudflare's// logging service may or may not completectx.waitUntil(postLog(err.toString()));conststack=JSON.stringify(err.stack)||err;// Copy the response and initialize body to the stack traceresponse=newResponse(stack,response);// Add the error stack into a header to find out what happenedresponse.headers.set("X-Debug-stack",stack);response.headers.set("X-Debug-err",err);}returnresponse;},};interfaceEnv{}exportdefault{asyncfetch(request,env,ctx):Promise<Response>{// Service configured to receive logsconstLOG_URL="https://log-service.example.com/";asyncfunctionpostLog(data){returnawaitfetch(LOG_URL,{method:"POST",body:data,});}letresponse;try{response=awaitfetch(request);if (!response.ok&&!response.redirected){constbody=awaitresponse.text();thrownewError("Bad response at origin. Status: "+response.status+" Body: "+// Ensure the string is small enough to be a headerbody.trim().substring(0,10),);}}catch (err){// Without ctx.waitUntil(), your fetch() to Cloudflare's// logging service may or may not completectx.waitUntil(postLog(err.toString()));conststack=JSON.stringify(err.stack)||err;// Copy the response and initialize body to the stack traceresponse=newResponse(stack,response);// Add the error stack into a header to find out what happenedresponse.headers.set("X-Debug-stack",stack);response.headers.set("X-Debug-err",err);}returnresponse;},}satisfiesExportedHandler<Env>;from workersimport WorkerEntrypointfrom pyodide.ffiimport create_proxyfrom jsimport Response, fetchasyncdefpost_log(data):log_url="https://log-service.example.com/"awaitfetch(log_url,method="POST",body=data)classDefault(WorkerEntrypoint):asyncdeffetch(self,request):# Service configured to receive logsresponse=awaitfetch(request)try:ifnot response.okandnot response.redirected:body=await response.text()# Simulating an error. Ensure the string is small enough to be a headerraiseException(f'Bad response at origin. Status:{response.status} Body:{body.strip()[:10]}')exceptExceptionas e:# Without ctx.waitUntil(), your fetch() to Cloudflare's# logging service may or may not completeself.ctx.waitUntil(create_proxy(post_log(str(e))))# Copy the response and add to headerresponse= Response.new(stack, response)response.headers["X-Debug-err"]=str(e)return responseimport{Hono} from'hono';// Define the environment with appropriate typesinterfaceEnv{}constapp=newHono<{Bindings:Env}>();// Service configured to receive logsconstLOG_URL="https://log-service.example.com/";// Function to post logs to an external serviceasyncfunctionpostLog(data:string){returnawaitfetch(LOG_URL,{method:"POST",body:data,});}// Middleware to handle error loggingapp.use('*',async(c,next)=>{try{// Process the request with the next handlerawaitnext();// After processing, check if the response indicates an errorif (c.res&& (!c.res.ok&&!c.res.redirected)){constbody=awaitc.res.clone().text();thrownewError("Bad response at origin. Status: "+c.res.status+" Body: "+// Ensure the string is small enough to be a headerbody.trim().substring(0,10));}}catch (err){// Without waitUntil, the fetch to the logging service may not completec.executionCtx.waitUntil(postLog(err.toString()));// Get the error stack or error itselfconststack=JSON.stringify(err.stack)||err.toString();// Create a new response with the error informationconstresponse=c.res?newResponse(stack,{status:c.res.status,headers:c.res.headers}):newResponse(stack,{ status:500});// Add debug headersresponse.headers.set("X-Debug-stack",stack);response.headers.set("X-Debug-err",err.toString());// Set the modified responsec.res=response;}});// Default route handler that passes requests throughapp.all('*',async(c)=>{returnfetch(c.req.raw);});exportdefaultapp;