6

In the documentation Using JavaScript Promises it is mentioned

To show an error message in a catch() method, use $A.reportError()

Why would I want to use $A.reportError that at all? I could use console.error(), for instance, to report all errors caught in catch.

Also in that docs I read the following statement

If you don't have a catch() method, keep an eye on your browser’s console during development for reports about uncaught errors in a promise.

This phrase during development for reports about uncaught errors made me wonder, does it mean that we need catch() and $A.reportError for a developer to be able to conveniently spot any unhanded errors during development? And theoretically, in production we shouldn't even get these unhanded errors.

3
+50

It seems that $A.reportError does a little more than just report the error on the console. It gets the action of the exception, if available, and uses that as description. It will also wrap a non-Aura error, so that required information can be set to the error message.

It seems that it will also post the error to the server:

Post the action failure to the server, where we can keep track of it for bad client code. But don't keep re-posting if the report of failure fails.

This is from the Logger.js file on the archived Aura repository on GitHub.


As for "why would I use that instead of console.error?" my best bets would be that:

  1. since it posts something to the server, it will probably get logged somewhere, like on an EventLogFile object;
  2. it will be handled by the framework, instead of just showing the error message on the console (it will probably pop that modal showing a "component error" message).

The second one is more likely, because the documentation says:

Throwing an error in a promise doesn’t trigger window.onerror, which is where the framework configures its global error handler.

And if this is the case, then it will show the popup to the current user, which is likely undesirable, but it will provide something that the user can show/copy to the org administrator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.