Skip to content

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

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

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

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

createPolicyChain discarded the return value of applyPolicy, causing the returned function to return undefined instead of a Promise. The generated router calls .catch() on dispatch(...), which threw TypeError: Cannot read properties of undefined (reading 'catch') whenever any policy was configured.

Changes

  • src/helpers/router.ts: Made applyPolicy return Promise<void>. Base case wraps out(...) in Promise.resolve() for proper error propagation. Recursive case uses new Promise with try/catch for synchronous policy errors, chaining downstream via .then(resolve, reject). The returned function now returns the promise from applyPolicy.
  • generated-defs/helpers/router.ts: Regenerated to reflect the source change.
  • test/router.test.ts: Added 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] });
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
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

2 participants