Meteor.Error message

Curious, is there any reason why the message in the Meteor Error class is wrapped in brackets?

I’m using it like this:

throw new Meteor.Error("the error message")

And when I retrieve from a method callback, e.g. err.message, it displays [the error message]

Looks like err.error will provide the error string as it was passed in but err.message feels nicer semantically.

It’s because the Meteor.Error constructor is meant to accept a “code” as its first argument, with the human-readable message being the second argument. So, the intended usage is something like:

throw new Meteor.Error('my-code', 'This is my error message');

That will give you an error message like this:

This is my error message [my-code]

And if you use the error’s reason field instead, it doesn’t include the code in brackets:

This is my error message
1 Like

Thanks for your help! I took another look at the meteor docs and source code too. Sounds like I should change how I’m handling errors on the client side.

If anyone has any best practices to share on that, would be interested to learn.

On that note, while the second parameter of Meteor.Error, the reason, is documented as being of type string, you can actually pass an object too, and, even if thrown on the server, the client will receive it.

This comes quite handy if you intend to display a more elaborate message with various context information about the error occurred, and especially if you need to do it in various languages.