where cookies??? where res?

Hi everyone, I'm not using cookies at all, or trying to mess with the headers myself and am getting this error in a POST handler: [NOTE]: i'm porting this from express to hono and the logic hasn't changed and this has been working forever (in express) [ALSO]: the user is created in the database, the email is sent and received... this is my code:
export const inviteUserHandler = async (c) => {
const { id: invitedBy } = c.user;
const now = Date.now().valueOf();
const values = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };

await db.transaction(async (tx) => {
try {
const existingUser = await tx.query.Users.findFirst({
where: eq(Users.email, c.body.email),
});
let dbUserId = null;

if (existingUser) {
if (!['Active', 'Blocked'].includes(existingUser.status)) {
await tx.update(Users).set(values).where(eq(Users.id, existingUser.id)).returning();
dbUserId = existingUser.id;
} else {
return c.text('USER ALREADY ACTIVE ON THE PLATFORM', 400);
}
} else {
const newUser = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };
await tx.insert(Users).values(values);
const dbUser = await tx.query.Users.findFirst({
where: eq(Users.email, c.body.email),
with: { company: true, inviter: true },
});

await sendInvite(dbUser);
}
return c.json({ ok: true });
} catch (ex) {
console.log(ex);
return c.text('Error inviting user', 501);
}
});
};
export const inviteUserHandler = async (c) => {
const { id: invitedBy } = c.user;
const now = Date.now().valueOf();
const values = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };

await db.transaction(async (tx) => {
try {
const existingUser = await tx.query.Users.findFirst({
where: eq(Users.email, c.body.email),
});
let dbUserId = null;

if (existingUser) {
if (!['Active', 'Blocked'].includes(existingUser.status)) {
await tx.update(Users).set(values).where(eq(Users.id, existingUser.id)).returning();
dbUserId = existingUser.id;
} else {
return c.text('USER ALREADY ACTIVE ON THE PLATFORM', 400);
}
} else {
const newUser = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };
await tx.insert(Users).values(values);
const dbUser = await tx.query.Users.findFirst({
where: eq(Users.email, c.body.email),
with: { company: true, inviter: true },
});

await sendInvite(dbUser);
}
return c.json({ ok: true });
} catch (ex) {
console.log(ex);
return c.text('Error inviting user', 501);
}
});
};
No description
15 Replies
Nico
Nico6mo ago
Are you using app.onError, if not turn that on and console log the error. It might give you a better error message
rubberduckies
rubberduckiesOP6mo ago
hey @Nico , thanks for the assist. I finally figured out what was happening and was now coming back here to report on it. app.onError catches everything that isn't solved by a try catch? that is extremely useful, thanks the answer to this problem has to do with the simple fact that i wasn't returning my await db.transaction(async (tx) => { so,
export const inviteUserHandler = async (c) => {
const { id: invitedBy } = c.user;
const now = Date.now().valueOf();
const values = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };

return db.transaction(async (tx) => {
//....
return c.json({ ok: true );
});
};
export const inviteUserHandler = async (c) => {
const { id: invitedBy } = c.user;
const now = Date.now().valueOf();
const values = { ...c.body, status: 'Invited', invitedBy, createdAt: now, updatedAt: now };

return db.transaction(async (tx) => {
//....
return c.json({ ok: true );
});
};
the cryptic error, with _res.headers happens everytime in hono when a handler doesn't return a response
Nico
Nico6mo ago
I'm not sure why it's showing that, I do usually see it say a response was not retuned
rubberduckies
rubberduckiesOP6mo ago
it may be the fact i'm using Bun.serve i have noticed the error logging in bun needs a lot of work but yeah, not sure
Nico
Nico6mo ago
And yes if you use app.onError it will catch all unhandled errors So in my preference I don't use any try/catch inside a route or middleware. I have a error handler to see all the erorrs and throw the proper response It might be a ts thing too
rubberduckies
rubberduckiesOP6mo ago
i will definitely work on implementing that this weekend, before it becomes too difficult what happens to the response, on app.onError? can we still return a 501?
Nico
Nico6mo ago
I'll give you an example: Look at this error handler https://github.com/phxlab/gametime-server/blob/main/src/lib/errors/mongoose.ts And it's handled in this app.onError https://github.com/phxlab/gametime-server/blob/main/src/app.ts I can still throw a default error if nothing matches
rubberduckies
rubberduckiesOP6mo ago
yeah, that's perfect :;
Nico
Nico6mo ago
GitHub
GitHub - NicoPlyley/hono-error-handler
Contribute to NicoPlyley/hono-error-handler development by creating an account on GitHub.
rubberduckies
rubberduckiesOP6mo ago
getting happier and happier with hono
Nico
Nico6mo ago
Here's the error handler I made if you want to use it. I need to update the documentation a bit I made it in a rush
rubberduckies
rubberduckiesOP6mo ago
will definitely look into it are you part of the hono team? or just a helping angel?
Nico
Nico6mo ago
Just a helper and contributor. I help most by answering questions and managing the Discord
rubberduckies
rubberduckiesOP6mo ago
well thank you
Nico
Nico6mo ago
No problem! It's what I enjoy doing
Want results from more Discord servers?
Add your server