H
Hono2w ago
avem

Pass context to exception handler

Hey, is there a way to pass some context about request to the exception handler via Hono context?
2 Replies
avem
avem2w ago
// Example route
app.get('/', async (c) => {
// ....computing the context...
const context = {
extensionID: params.extension_id,
fingerprint: params.fingerprint,
plan: account.plan(),
accountID: account.id(),
extensionVersion: params.extension_version,
currentPhoto: account.totalUsage() + 1,
clerkID: clerkID,
aiMode: params.ai_mode,
solveMode: params.solve_mode,
language: params.language,
joinedAt: account.createdAt(),
};

// ...some exception happening..
});

// Custom error handler
app.onError((err, c) => {
// I want to have access to it in here via "c"
});
// Example route
app.get('/', async (c) => {
// ....computing the context...
const context = {
extensionID: params.extension_id,
fingerprint: params.fingerprint,
plan: account.plan(),
accountID: account.id(),
extensionVersion: params.extension_version,
currentPhoto: account.totalUsage() + 1,
clerkID: clerkID,
aiMode: params.ai_mode,
solveMode: params.solve_mode,
language: params.language,
joinedAt: account.createdAt(),
};

// ...some exception happening..
});

// Custom error handler
app.onError((err, c) => {
// I want to have access to it in here via "c"
});
I guess the only way of doing it is to wrap exception and add context? I would like to avoid that since, some of the libraries I use throw exceptions in uncaught promises, which don't allow me to catch them in handler try/catch block.
Aditya Mathur
Aditya Mathur2w ago
You can pass this data in the Exception as well as set this in context using .set and access it in the onError using .get
Want results from more Discord servers?
Add your server