title: How to properly handle error on mongoose CRUD
body: <p>I am building a very simple RESTFUL app to see how Node and Mongo work together.</p>
  
  <p>A made a function to find a "Persona" document on a mongodb by Id, so here is the code:</p>
  
  <pre><code>function getPersonabyId(req,res)
  {
      //en params del objeto request esta lo que llega por get
      let idPersona=req.params.id;
  
      //mongoose trae un método para pillar por id
      Persona.findById(idPersona).exec((err,objPersona)=&gt;
      {
  
          if(err)
          {
             res.status(500).send(err);
          }
          else{
  
  
              res.status(200).send(objPersona);
          }
  
      })
  
  
  }
  </code></pre>
  
  <p>And I use this route, where function is executed:</p>
  
  <pre><code>miniApp.get("/getPersonaById/:id",controladorPersona.getPersonabyId);
  </code></pre>
  
  <p>So, when I pass as parameter a valid ID, everything is ok, I get the proper Persona object in the response.</p>
  
  <p>But when I use a not valid ID (no document with that ID exists on the mongodb), the error specified on the exec callback is thrown... but, isn´t supossed that this error should be thrown only in case there are server problems? a non existent ID should not be a 500 error, isn´t it?</p>
  
  <p>I looked for info and I found this:</p>
  
  <p><a href="https://coursework.vschool.io/mongoose-crud/" rel="nofollow noreferrer">https://coursework.vschool.io/mongoose-crud/</a></p>
  
  <pre><code>Person.find((err, people) =&gt; {  
      // Note that this error doesn't mean nothing was found,
      // it means the database had an error while searching, hence the 500 status
      if (err) return res.status(500).send(err)
      // send the list of all people
      return res.status(200).send(people);
  });
  </code></pre>
  
  <p>And reading the commented lines on the code above, confuses me even more... the error, as said in those comments, should be an error database or something similar, not a "not found" error... but the error is  actually thrown when an object with that ID is not found!</p>
  
  <p>This is the error I get with a non valid ID:</p>
  
  <pre><code>{
      "message": "Cast to ObjectId failed for value \"5b105ba453401c41d0e3da2\" at path \"_id\" for model \"Persona\"",
      "name": "CastError",
      "stringValue": "\"5b105ba453401c41d0e3da2\"",
      "kind": "ObjectId",
      "value": "5b105ba453401c41d0e3da2",
      "path": "_id"
  }
  </code></pre>