Skip to content

DispatchNamespace.get does not throw on missing worker as documented #5263

@timkelty

Description

@timkelty

What versions & operating system are you using?

System:
OS: macOS 26.0.1
CPU: (10) arm64 Apple M1 Max
Memory: 666.42 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.17.1 - /Users/timkelty/.nvm/versions/node/v22.17.1/bin/node
npm: 10.9.2 - /Users/timkelty/.nvm/versions/node/v22.17.1/bin/npm
pnpm: 8.15.4 - /opt/homebrew/bin/pnpm
npmPackages:
@cloudflare/vitest-pool-workers: ^0.8.19 => 0.8.71
wrangler: ^4.42.1 => 4.42.1

Please provide a link to a minimal reproduction

No response

Describe the Bug

The generated type definition looks like this:

interface DispatchNamespace {
    /**
    * @param name Name of the Worker script.
    * @param args Arguments to Worker script.
    * @param options Options for Dynamic Dispatch invocation.
    * @returns A Fetcher object that allows you to send requests to the Worker script.
    * @throws If the Worker script does not exist in this dispatch namespace, an error will be thrown.
    */
    get(name: string, args?: {
        [key: string]: any;
    }, options?: DynamicDispatchOptions): Fetcher;
}

However, the @throws If the Worker script does not exist in this dispatch namespace, an error will be thrown. appears to be inaccurate.

Given a hello-world in the matching namespace, this dispatch worker will throw with a "worker not found" exception, as is not thrown until fetch is called:

export default {
	async fetch(request: Request, env: Env): Promise<Response> {
		let worker;

		try {
			worker = env.DISPATCHER.get('does-not-exist');
		} catch (cause) {
			worker = env.DISPATCHER.get('hello-world');
		}

		return await worker.fetch(request);
	},
} satisfies ExportedHandler<Env>;

With the behavior as-is, you're forced to wrap the fetch in a try/catch, at which point you can't distinguish between a missing worker and an actual exception thrown from the fetch.

Feels like a bug, and it should actually work how it is documented.

Please provide any relevant error logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions