madhums / node-express-mongoose-demo

A simple demo app using node and mongodb for beginners (with docker)

Home Page:https://nodejs-express-demo.fly.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catch block only checks 422 validation error

novaknole opened this issue · comments

Hello. Here is code I found in your project.

try {
    yield article.uploadAndSave(req.file);
    req.flash('success', 'Successfully created article!');
    res.redirect(`/articles/${article._id}`);
  } catch (err) {
    res.status(422).render('articles/new', {
      title: article.title || 'New Article',
      errors: [err.toString()],
      article
    });
  }

So, the problem I see is that let's save uploadAndSave method throws an exception , but it's not 422 validation error. What this code will do is that even though thrown error is not 422, it will think it is 422 and redirect to articles/new url. I think this is not the best way and might be wrong.

Yes, you might be right. The error handling can be more informative BUT this is just a demo application. Feel free to customise as per your use case.