Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app crashing when bot code causes an error #64

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,28 @@ const DefaultRouteRequest = z.object({
});

const defaultRouteHandler: RequestHandler<{}, {}, z.infer<typeof DefaultRouteRequest>> = async (req, res, next) => {
const { step } = DefaultRouteRequest.parse(req.body);
if (!step) {
throw new Error('no step');
try {
const { step } = DefaultRouteRequest.parse(req.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kindof wondering if maybe the try/catch should only wrap this line? Like below you're throwing in a couple of other cases that this new block will catch. I'm not sure that's what you want to happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the step variable being used in the rest of the lines (except the await handler line), wouldn't they all have to be in the try block because of this? I was getting an error by only having the first line in try I think. I can move await handler outside the try catch block

if (!step) {
throw new Error('no step');
}
const handler = Object.prototype.hasOwnProperty.call(steps, step) && steps[step];
if (!handler) {
throw new Error(`invalid step ${step}`);
}

await handler(req, res, next);
}

const handler = Object.prototype.hasOwnProperty.call(steps, step) && steps[step];
if (!handler) {
throw new Error(`invalid step ${step}`);
catch (e) {
if (e instanceof Error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what else might e be an instance of here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yep - I don't need this line

logger.log('error', `error while parsing the request`);
}

}

await handler(req, res, next);



};

router.post('/', defaultRouteHandler);
Expand Down
Loading