Skip to content

fix(http-server-js): createPolicyChain returns a Promise when policies are present#9917

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-global-policy-error
Draft

fix(http-server-js): createPolicyChain returns a Promise when policies are present#9917
Copilot wants to merge 4 commits intomainfrom
copilot/fix-global-policy-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

Using global policies with the generated router caused TypeError: Cannot read properties of undefined (reading 'catch') because createPolicyChain returned a function that returned undefined instead of a Promise. The generated router calls .catch() on the dispatch result, which fails when no Promise is returned.

Changes

  • src/helpers/router.ts: Made applyPolicy return Promise<void>. The base case wraps out(...) in Promise.resolve(). The recursive case uses new Promise with try/catch to handle synchronous policy errors, chaining downstream via .then(resolve, reject). The returned wrapper function now propagates the Promise.
  • generated-defs/helpers/router.ts: Regenerated to reflect the source change.
  • test/router.test.ts: Added unit tests covering Promise return, policy execution order, error propagation from out, synchronous policy throws, and request forwarding.
// Previously threw: TypeError: Cannot read properties of undefined (reading 'catch')
const policy: Policy = async (ctx, next) => { next(); };
const router = createDemoServiceRouter({...}, { policies: [policy] });

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build node pec/�� tput-dir ../../w--llmstxt sh _modules/.bin/no../../website/src/content/docs/docs/emitters/openapi3/reference --no-emit ture dist/node-gyp-bigenerate-scenarios-summary sed tobu�� s,\\,/,g sh /node_modules/.bin/sh import @typespecnode ture tobuf/reference node (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[http-server-js][Bug]: adding a global policy throws an error</issue_title>
<issue_description>### Describe the bug

When using http-server-js with a custom global policy, an error is thrown.

const policy: Policy = async (ctx, next) => {
  next();
}
const router = createDemoServiceRouter({...}, { policies: [policy] });
// ...

this will always throw TypeError: Cannot read properties of undefined (reading 'catch')

Reproduction

Here is a repo with the reproduction of each case https://github.com/benjlevesque/repro-typespec-issues/blob/main/policy.test.ts

Resolution lead

The bug seems to be in the implementation of createPolicyChain, which does not return a Promise when at least one Policy is specified.
Adding async or returning Promise.resolve fixes the problem

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…specified

Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error when adding a global policy in http-server-js fix(http-server-js): createPolicyChain returns Promise when policies are present Mar 4, 2026
Copilot AI and others added 2 commits March 9, 2026 20:16
Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
Copilot AI changed the title fix(http-server-js): createPolicyChain returns Promise when policies are present fix(http-server-js): createPolicyChain returns a Promise when policies are present Mar 9, 2026
* @param policies - The policies to apply to the request.
* @param out - The function to call after the policies have been applied.
*/
export function createPolicyChain<Out extends (ctx: HttpContext, ...rest: any[]) => void>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the result function of createPolicyChain is always awaited, I feel Out should always return a promise, so that the result is necessarily a promise, whether there's a policy or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-server-js][Bug]: adding a global policy throws an error

3 participants