Skip to content

Commit d39ea95

Browse files
committed
🔧 fix: #1177 cookie does not sign when an error is thrown
1 parent 4bc4ecd commit d39ea95

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Improvement:
3636
- support resolve macro on ws
3737
- [#1146](https://github.com/elysiajs/elysia/pull/1146) add support to return web API's File from handler
3838
- [#1165](https://github.com/elysiajs/elysia/pull/1165) skip non-numeric status codes in response schema validation
39+
- [#1177](https://github.com/elysiajs/elysia/issues/1177) cookie does not sign when an error is thrown
3940

4041
Bug fix:
4142
- `Response` returned from `onError` is using octet stream

example/a.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import { Elysia, t } from '../src'
22

33
const app = new Elysia()
4-
.macro({
5-
a: {
6-
resolve: () => ({
7-
a: 'a'
4+
.onError(({ code, error }) => {
5+
console.error('[error]', error)
6+
return { error: { code } }
7+
})
8+
.get(
9+
'/session',
10+
({ error, cookie: { sessionToken } }) => {
11+
const refreshed = !!sessionToken.value
12+
13+
sessionToken.set({
14+
value: Math.random().toString(36).substring(2, 8),
15+
maxAge: 1000 * 60 * 60 * 24 * 7
816
})
17+
18+
if (refreshed) throw error('Unauthorized')
19+
20+
return sessionToken.value
21+
},
22+
{
23+
cookie: t.Cookie(
24+
{ sessionToken: t.Optional(t.String()) },
25+
{
26+
sign: ['sessionToken'],
27+
secrets: 'my-secret'
28+
}
29+
)
930
}
10-
})
11-
.get('/a', ({ a }) => {}, {
12-
a: true,
13-
beforeHandle: ({ query }) => {}
14-
})
15-
.ws('/', {
16-
a: true,
17-
message({ data: { a } }) {}
18-
})
31+
)
32+
.listen(3000)
33+
34+
console.log(app.routes[0].compile().toString())

src/compose.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,8 @@ export const composeHandler = ({
18731873
`const set=c.set\n` +
18741874
`if(!set.status||set.status<300)set.status=error?.status||500\n`
18751875

1876+
if (hasCookie) fnLiteral += encodeCookie()
1877+
18761878
if (hasTrace && hooks.trace)
18771879
for (let i = 0; i < hooks.trace.length; i++)
18781880
// There's a case where the error is thrown before any trace is called

0 commit comments

Comments
 (0)