microsoft / microsoft/typespec
[http-server-js][Bug]: adding a global policy throws an error
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Describe the bug
When using http-server-js with a custom global policy, an error is thrown.
```ts
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`](https://github.com/microsoft/typespec/blob/39a1ac37eaafb4370bc27c9470b28b8cc655cf95/packages/http-server-js/src/helpers/router.ts#L63), which does not return a Promise when at least one Policy is specified.
Adding `async` or returning `Promise.resolve` fixes the problem
Contributor guide
Assessment
This issue has not been assessed yet.