Ticket: Error Handling / Error Handling lecture

So, I only passed this ticket because I threw in a return null as my piece of code to pass the test, purely so I could see the answer.

I was a little bit surprised how light lecture was on detail on what is a key part of development.

  1. Does catch(e) only ever contain an error message? For example on this ticket

“Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters”

} catch (e) {
console.log(e.toString())
}

Since this only seemed on contain the error message.

  1. Is there no way to grab a error code when mongoDB throws an exception. I was imaging an error object with codes and stuff to trap.

  2. Does anyone know of any better pieces on the handling errors with a node/mongo code framework. More realistic pieces of code to hand issues when interfacing with Mongodb.

  3. Finally why does using: -

npm test -t error-handling

Run

test/error-handling.test.js

and

test/lessons/writes-with-error-handling.spec.js

Is this an npm thing?

2 Likes

Ok,

So if someone could expand on this, why does catch of a mongo error/exception sometimes return just an error message and sometimes an error object?

  1. So this, where the id = “helloworld” , which is from within getMovieById → movieDAO.js

try {
const pipeline = [
{ $match: { _id: ObjectId(id) } },

    { $lookup: {
        from: 'comments',
        let: {'id': '$_id'},
        pipeline: [
          { $match: { '$expr': {'$eq': [ '$movie_id', '$$id']} }  },
          { $sort: {'date': -1}  }  
        ],
        as: 'comments'
      }
    }
  ]
  return await movies.aggregate(pipeline).next()
} catch (e) {
   console.log(e)

Returns :-

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

  1. And this from within writes-with-error-handling.spec.js
// and what if we tried to insert a document with the same _id?

try {
  let dupId = await errors.insertOne({ _id: 0,  })
} catch (e) {
  console.log(e)

Returns

  MongoError: E11000 duplicate key error collection: sample_mflix.errors index: _id_ dup key: { _id: 0 }
      at Function.create (C:\Dropbox\Trusted\MongoDB\M220JS - MongoDB for Javascript Developers\mflix-js\node_modules\mongodb\lib\core\error.js:43:12)
      at toError (C:\Dropbox\Trusted\MongoDB\M220JS - MongoDB for Javascript Developers\mflix-js\node_modules\mongodb\lib\utils.js:150:22)
      at C:\Dropbox\Trusted\MongoDB\M220JS - MongoDB for Javascript Developers\mflix-js\node_modules\mongodb\lib\operations\common_functions.js:266:39
      at C:\Dropbox\Trusted\MongoDB\M220JS - MongoDB for Javascript Developers\mflix-js\node_modules\mongodb\lib\core\connection\pool.js:420:18
      at processTicksAndRejections (internal/process/task_queues.js:77:11) {
    driver: true,
    index: 0,
    code: 11000,
    keyPattern: { _id: 1 },
    keyValue: { _id: 0 },
    errmsg: 'E11000 duplicate key error collection: sample_mflix.errors index: _id_ dup key: { _id: 0 }',
    [Symbol(mongoErrorContextSymbol)]: {}
  }

Hmmmm - Is one a Mongodb driver error and one a Mongodb error?

1 Like

same problem, error for invalid id is not a well-formatted one so it seems we have to copy the whole string if we want to test it.

and by the way, both should be coming from the server after a write operation I think.