My worker showing error - Worker threw exception

HI,
I have an account with the Cloudflare my worker is start showing Worker threw exception error and i am not able to create a worker also on my account. On another account worker is working fine for me.

Here is the url; cold-thunder-77b3.247workonpc.workers.dev

Is the issue is with the billing because 1 day my limit was reached.

Can you share your worker code? Do you see any errors in the logs? Does it run locally?

https://developers.cloudflare.com/workers/learning/debugging-workers should be a good first step. Then check out https://developers.cloudflare.com/workers/learning/logging-workers/ to see what the exact exception logs are. With that information, we’ll be able to advise and help much more easily.

1 Like

addEventListener(‘fetch’, event => {

event.respondWith(handleRequest(event.request))

})

async function handleRequest(request) {

let blockedIPs = [‘0.0.0.0’]

let requestIP = request.headers.get(‘cf-connecting-ip’)

if (blockedIPs.includes(requestIP)) {

return new Response('Access denied', {

  status: 403

})

}

const upstreamMobileHost = ‘totalsportek dot to’

const upstreamDesktopHost = ‘totalsportek dot to’

let userAgent = request.headers.get(‘user-agent’)

let upstreamHost = await isMobileUserAgent(userAgent) ?

upstreamMobileHost : upstreamDesktopHost

let forwardHeaders = new Headers(request.headers)

let url = new URL(request.url)

url.host = upstreamHost

forwardHeaders.set(‘Host’, url.hostname)

forwardHeaders.set(‘Referer’, url.hostname)

let upstreamResponse = await fetch(url.href, {

method: request.method,

headers: forwardHeaders

})

let responseHeaders = new Headers(upstreamResponse.headers)

responseHeaders.set(‘Cache-Control’, ‘no-store’)

responseHeaders.set(‘access-control-allow-origin’, ‘*’)

responseHeaders.set(‘access-control-allow-credentials’, true)

responseHeaders.delete(‘content-security-policy’)

responseHeaders.delete(‘content-security-policy-report-only’)

responseHeaders.delete(‘clear-site-data’)

let contentType = responseHeaders.get(‘content-type’)

let responseText = contentType.includes(‘text/html’) && contentType.includes(‘UTF-8’) ?

await filterResponse(upstreamResponse, upstreamHost, url.hostname) :

upstreamResponse.body

return new Response(responseText, {

status: upstreamResponse.status,

headers: responseHeaders

})

}

async function filterResponse(response, oldPattern, newPattern) {

let filterRules = {oldPattern: newPattern, ‘//totalsportek dot to’: ‘’}

let text = await response.text()

for (let rule in filterRules) {

let regex = new RegExp(rule, 'g')

text = text.replace(regex, filterRules[rule])

}

return text

}

async function isMobileUserAgent(userAgent) {

const keywords = [“Android”, “iPhone”, “iPad”, “Windows Phone”]

return keywords.filter(keyword => userAgent.match(keyword)).length != 0

}

The same code works perfectly with another Cloudflare account.

Even fresh new workers showing me the same errors with the Cloudflare code.

My all new worker with the default code also showing me same error. Is is due to Cloudflare billing issue or what?

View the logs of the Worker when you hit the exception.

In my case, your code fails because of "message": "Invalid hostname when stringifying URL.",

Specifically, these aren’t valid hostnames.

const upstreamMobileHost = 'totalsportek dot to'

const upstreamDesktopHost = 'totalsportek dot to'

If it was billing related, you’d receive a 1027 error page.

image