As a beginner in web development, I've recently come across an interesting observation related to handling errors. When working with json.stringyfy()
, I noticed that the message
key is not displayed in statement 2. However, accessing error.message
returns a value instead of undefined
.
try {
//Some error occours
} catch (error) {
console.log(JSON.stringify(error)) //statement 1
console.error(error) //statement 2
console.log(error.message) //statement 3
console.log(Object.keys(error)) //statement 4
}
statement 1 logs
MongoServerError: E11000 duplicate key error collection: trendyApp.Markets index: name_1 dup key: { name: "murat market" }
at D:\web projects\trendyApp\server\node_modules\mongodb\lib\operations\insert.js:51:33
at D:\web projects\trendyApp\server\node_modules\mongodb\lib\cmap\connection_pool.js:273:25
at handleOperationResult (D:\web projects\trendyApp\server\node_modules\mongodb\lib\sdam\server.js:363:9)
at MessageStream.messageHandler (D:\web projects\trendyApp\server\node_modules\mongodb\lib\cmap\connection.js:474:9)
at MessageStream.emit (events.js:375:28)
at processIncomingData (D:\web projects\trendyApp\server\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (D:\web projects\trendyApp\server\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at TLSSocket.ondata (internal/streams/readable.js:726:22) {
index: 0,
code: 11000,
keyPattern: { name: 1 },
keyValue: { name: 'murat market' }
}
statement 2 logs
{"index":0,"code":11000,"keyPattern":{"name":1},"keyValue":{"name":"murat market"}}
statement 3 logs
E11000 duplicate key error collection: trendyApp.Markets index: name_1 dup key: { name: "murat market" }
While developing an express application and encountering this error from Mongoose, I believe this behavior is prevalent across JavaScript.
statement 4 logs
[ 'index', 'code', 'keyPattern', 'keyValue' ]