500 Internal Error Cloudflare Workers

Cloudflare Workers can be a huge pain to debug since the code is running remotely and error messages can be extremely sparse (bordering on completely useless).

If you're getting an error message "Error: Internal Error" with an error code of 500 from a Cloudflare Workers instance, there's a good chance that there's a syntax or reference error (referencing an undefined or null object) in your code.

I spent many hours debugging this issue (multiple times) and want to point people in the right direction.


I recommend resetting returning early with something like below if you're already running a remote logger and are still having trouble:

return new Response('Got here ' + Date.now(), {
    status: 200,
})


I personally use debugging.exitEarly() for this in my code when I'm having issues with the 500 Error: Internal Error.

const debugging = {
    // returns from the Cloudflare Worker thread with a debugging response and the current time
    exitEarly: async function () {
        return new Response(`stopped early for debugging ` + Date.now(), {
            status: 506,
        })
    },
}


Also, be wary of Array.forEach(), because it's asyncronous and your function may/will return the default fall-through rather than the return values from the forEach call.

Best of luck,
Nanch



Please submit feedback or corrections to: nanch at nanch.com