Error handling (systematically and consistently) #64
Replies: 5 comments 1 reply
-
According to the concept above, we can design two classes: one for business errors and the other for system errors. Each class should have two attributes: class BusinessError extends Error {
code: string;
constructor(code: string,message: string) {
super(message);
this.code = code;
}
} class SystemError extends Error {
code: string;
constructor(code: string,message: string) {
super(message);
this.code = code;
}
} Define error handling middleware app.onError((err, c) => {
if (err instanceof BusinessError) {
// business logic error
return c.json(
{
error: {
code: err.code,
message: err.message,
},
},
err.status
);
} else if (err instanceof SystemError) {
// system error
logger.error('System Error:', err);
return c.json(
{
error: {
code: err.code,
message: 'An unexpected error occurred. Please try again later.',
},
},
err.status
);
} else {...} route example app.post('/v0/status/:id', async (c) => {
if (!id) {
throw new BusinessError(...);
}
// database operation
try {
// assume there is connection error
throw new Error('Database connection failed');
} catch (err) {
throw new SystemError(...);
}
return c.json({ ... });
}); |
Beta Was this translation helpful? Give feedback.
-
I believe there are two types of errors:
I’ll classify them as externalErrors and internalErrors.
Reference:
Example: console.error("Error SCOPE-01: Cannot connect to db");
console.debug("Serving request xxxxxxx"); |
Beta Was this translation helpful? Give feedback.
-
I agree with what you said. The reason for defining two types of Error classes is as follows:
|
Beta Was this translation helpful? Give feedback.
-
Both names, UserError and SystemError, are fine. If UserErrors or SystemErrors are used, they are likely to be registries for storing multiple entries. pseudo code class UserError extends Error {
code: string;
constructor(code: string,message: string) {
super(message);
this.code = code;
}
}
class SystemError extends Error {
constructor(message: string) {
super(message);
this.code = 500;
}
} |
Beta Was this translation helpful? Give feedback.
-
Builtin debugging for deno:https://deno.com/blog/zero-config-debugging-deno-opentelemetry Fly.io logging |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Normally, an application encounters two types of errors: business errors and system errors. A business error occurs when a user provides incorrect input parameters, for example. In such cases, we need to return appropriate information to the user so they understand what went wrong and what actions they should take.
The second type is a system error, such as a network failure. These are considered exceptions. We typically do not send detailed error information back to the user, as they cannot take any meaningful action to resolve it. Instead, we should log these errors for further analysis and troubleshooting.
Beta Was this translation helpful? Give feedback.
All reactions