Error handling problems while testing

DOn’t understand how to test the error in the trry/catch block
I tested on e.message MongoError
Then on

e.errmsg E11000 duplicate key error collection: sample_mflix.errors index: id dup key: { _id: 0 }

Added in the catch block
if (e.name == “InvalidID”) {
// we’re kinda expecting this sometimes
return null;
}

And stil have the message (npm test-t error-handling)

Get Comments › Handles invalid ID error correctly
Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Help required

Thx

LAurent

Hey @lbois

From the lab

Use the variable e to figure out if the invalid ID error is being thrown, and then return null in this case.

perhaps you could try to console.log(e) to see more information. Perhaps what you are expecting with if (e.name) need more attention.

Hi

I checked the logs and I replaced my if (e.name) in my code by

} catch (e) {
/**
Ticket: Error Handling

  Handle the error that occurs when an invalid ID is passed to this method.
  When this specific error is thrown, the method should return `null`.
  */
 console.log( e );
  // TODO Ticket: Error Handling
  // Catch the InvalidId error by string matching, and then handle it.
  if (e.errmsg.includes( "E11000 duplicate key error collection: sample_mflix.errors index: _id_ dup key: { _id: 0 }")) {
    console.log('I am here')
    // we're kinda expecting this sometimes
    return null;
  }

But i got the error on e.errmsg
● Get Comments › Handles invalid ID error correctly

TypeError: Cannot read property 'includes' of undefined

  344 |       // TODO Ticket: Error Handling
  345 |       // Catch the InvalidId error by string matching, and then handle it.
> 346 |       if (e.errmsg.includes( "E11000 duplicate key error collection: sample_mflix.errors index: _id_ dup key: { _id: 0 }")) {
      |           ^

I am puzzled

Laurent

The error message tells you that the property includes cannot be read for undefined. Since you call includes() from the expression e.errmsg it means e.errmsg is undefined. Probably because the object e does not have a property named errmsg. I would check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error for more information.

2 Likes

Test passes

Thanks all