H
Hono3mo ago
CrisOG

Middleware errors are not being returned in response type

I have a user route using an auth middleware,
export const usersRouter = new Hono()
.use(authorize)
.get('/', async (c) => {
const user = await User.getByUserID(c.var.auth.userID);

if (!user) {
return c.json({ error: 'User not found' }, 404);
}

return c.json(user, 200);
})
export const usersRouter = new Hono()
.use(authorize)
.get('/', async (c) => {
const user = await User.getByUserID(c.var.auth.userID);

if (!user) {
return c.json({ error: 'User not found' }, 404);
}

return c.json(user, 200);
})
And this is the auth middleware,
import { createMiddleware } from 'hono/factory';
import { lucia } from './lucia';

export const authorize = createMiddleware<{
Variables: {
auth: {
userID: string;
sessionID: string;
};
};
}>(async (c, next) => {
const cookieHeader = c.req.header('Cookie');

if (!cookieHeader) {
return c.json({ error: 'Unauthorized' }, 401);
}
// Code omitted ...
});
import { createMiddleware } from 'hono/factory';
import { lucia } from './lucia';

export const authorize = createMiddleware<{
Variables: {
auth: {
userID: string;
sessionID: string;
};
};
}>(async (c, next) => {
const cookieHeader = c.req.header('Cookie');

if (!cookieHeader) {
return c.json({ error: 'Unauthorized' }, 401);
}
// Code omitted ...
});
But, the response type has only 2 possible status code.
Argument of type '401' is not assignable to parameter of type '200 | 404'.ts(2345)
Argument of type '401' is not assignable to parameter of type '200 | 404'.ts(2345)
This is a problem because I write some tests that expect this error code when accessing an auth protected route without auth.
1 Reply
CrisOG
CrisOGOP3mo ago
Just found the issue about this on GitHub https://github.com/honojs/hono/issues/3170
GitHub
Hono doesn't consider status codes from middlewares with `testClien...
What version of Hono are you using? 4.4.9 What runtime/platform is your app running on? Bun What steps can reproduce the bug? // index.ts import { Hono } from "hono"; import { logger } fr...
Want results from more Discord servers?
Add your server