Lax — Apex Actions Exception Handing

It is going to defeat the callback hell nesting.

Note: This story is a part of “Intro to Lax” series. Stay tuned. There’s a lot more of this to come!
< Lax — The service Lightning Component to write a clear asynchronous JavaScript code


Lax provides an ability to handle all async code using native browser Promises. Below is an example of a simple action enqueueing to the server with a .catch() handler.

.catch() is a convenient way to handle errors in promise chains. As you can see from the code above, the .catch() function accepts a callback argument. This callback can be called in two situations. The first one is - when the server-side action return an "ERROR" state. But in the case when the action state is "SUCCESS", a .then() callback will be called. In this situation, the success callback can throw a client-side exception and the .catch() callback handles it.

Lax unifies all errors that come to .catch() callback functions by a JavaScript standard. It also provides several things specific to Lightning Component. The structure of an error passed to the action callback:

Note: When you call a .getError() function on the result from the server, it returns you the list of errors. But in most cases, there will be only one error. Lax writes this message to the standard JavaScript error message property. But, it also provides an ability to refer to standard errors using anentities property. However, to provide an access to the native return value, an error has an action property.

Error filtering

Lax provides .catch() in two variants:

  • catch-all — is similar to the synchronous standard JavaScript try/catch functionality (catch(e) {...}), it handles all error types
  • catch-specific — lets you handle only specific errors (like a strong types try/catch functionality - catch(DatabaseException e) {...})


.catch(function(any error) handler) -> Promise

Basically, the catch-all exception handler is a shortcut for calling .then(null, handler) on the promise. The catch-all handler calls when the server-side action return an "ERROR" or "INCOMPLETE" state. Also, an exception that happened in a .then()-chain will propagate to a nearest .catch() handler.


class ErrorClass|function(any error)|Object predicate...,
function(any error) handler
) -> Promise

This is an enhancement to .catch() to provide a filtering to the exception handling like in strong typed languages (Java, C#). Instead of manually checking for the type of an error (instanceof or .name === 'ApexActionError'), you can specify a list of error types which are available for .catch() handler. The .catch() handler that first gets appropriate error will be called.


  • Lax has own error types:
     — ApexActionError - the error contains a server-side action response with the "ERROR" state 
     — IncompleteActionError - the error specific to the "INCOMPLETE" server-side action state
  • There is an ability to register/catch a custom client-side error
  • The .catch() function can accept multiple types of error (.catch(TypeError, ReferenceError...)

.catch() accepts error types when the error constructor has its prototype property equal to instanceof Error. Below is simple custom error definition and the usage of it:


Lax provides several aliases to simplify the LaxPromise chaining even more.

  • .error() — it incapsulates the logic to assign ApexActionError on the .catch()handler
  • .incomplete() - is similar to .error() but, it incapsulates an IncompleteActionError assignment



If this article was useful for you, please sign it with an 👏 or share with your friends. It will force me to stay work on articles like that more, thanks!

Ruslan Kurchenko is an Advanced Salesforce Engineer from Kiev, Ukraine. He is passionate of Salesforce, JavaScript and Modern WEB Development. Connect with him on LinkedIn, Instagram or say Hi! on Twitter.