JS20 applies automatic error handling inside the run functions of the endpoints. At any time you can throw an error and the user-facing response will have status code 500 with your error message, or a generic "Unknown error" message.
app.addEndpoint({
method: 'GET',
path: '/error-example',
outputSchema: sMessage,
isLoggedIn: true,
run: async (system) => {
throw new Error("This error message will be user-facing");
}
});You can apply a custom error handler to catch any errors in the app and decide what to do with them:
async function handleError(error: any): ErrorResponse {
await saveToMyErrorLoggingService(error);
return {
error: "A custom error message",
code: 408
};
}
const app = new App({
handleError,
});