microsoft / microsoft/typespec

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

Open
#9,888 0 comments 0 reactions 0 assignees View on GitHub
bug emitter:service:js triaged:core
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.