TritonDataCenter / node-verror

Rich JavaScript errors
MIT License
1.18k stars 61 forks source link

Expose "jse_shortmsg" publicly #70

Open WangHansen opened 5 years ago

WangHansen commented 5 years ago

This is exactly the same request as the #52.

The use case is that I want to capture the entire error with all the causes for debugging, logging and other purposes. But when I return the JSON back to the client. I only want the user to see what is in the "jse_shortmsg".

For example:

// Example error from third-party library
const originalError = new MongoError("Duplicate keys detected")

// Example error I handle on my server
const error = VError(originalError, "Failed to update record")

// This would log: "Failed to update record: Duplicate keys detected"
logger.log(error.message)

// I only want user to see "Failed to update record"
res.json({ message: error["jse_shortmsg"] })
davepacheco commented 5 years ago

The conclusion in #52 was to use the WError class for this. Does that not do what you want?

WangHansen commented 5 years ago

@davepacheco Sorry I should have mentioned this. Yes, I did look at #52. The problem with using WError is that I lose the ability to get the chained message by calling error.message if I understand correctly.

The solution I was looking for is to provide a way to get both the chained long error message and the short version as well. It seems that with VError or WError, I can get either a short or a long version but not both.

davepacheco commented 5 years ago

Thanks for clarifying. I haven't thought about it much but it seems like we could add a method to WError to get the long message. I prefer that slightly to adding a short message to VError but either may be okay.

WangHansen commented 5 years ago

No problem, thanks so much for all the hard work, this is an amazing library!

I would prefer just adding it to VError just because I am mostly using VError throughout my code and I guess it would be the same for a lot of other developers. Maybe I am wrong. Works either way.