Elastic Path Commerce Development

Structured Error Messages from Commerce Engine

Structured Error Messages from Commerce Engine

Structured error messages are generated through the creation of constraint violations and invalid business states in Commerce Engine. These violations are then transformed into a set of com.elasticpath.common.dto.StructuredErrorMessage objects. Commerce Engine then generates an exception which returns both an exception message and the set of StructureErrorMessage objects and passes these to Cortex.

For more information on what kinds of messages Cortex returns, see the following:

Cortex Exception Handling

Cortex handles exceptions from Commerce Engine automatically using the ReactiveAdapter.fromService() method. Cortex returns the appropriate resource status based on whether it handling a InvalidBusinessStateException or a EpValidationException.

The following is an example of calling a Commerce Engine service with automatic exception handling.

  Observable o = reactiveAdapter.fromService(() -> ceService.myMethod());   

If an exception occurs StructuredErrorMessage objects in the exception are converted to Message or LinkedMessage objects. These message objects are wrapped in a ResourceOperationFailure, and returned on the Rx type's error channel. This process is transparent to the user.

In addition to being consumable by Cortex, converting StructuredErrorMessage objects to Message or LinkedMessage allows these objects to contain information about links.

StructuredErrorMessage Transformations in Cortex

StructuredErrorMessage objects passed from Commerce Engine are transformed and decorated with additional fields to make them consumable by cortex.

In Cortex, the set of StructuredErrorMessage objects are extracted from the exception and passed into a transformer, which transforms the objects into com.elasticpath.rest.advise.Message objects. This transformation replaces the Commerce Engine field names with front-end field names understandable by the client.

The resulting structure is attached to an execution result. The execution result moves up and is converted to an operation result. The final transformation is from Message to com.elasticpath.schema.StructuredMessage occurs in this stage. StructuredMessage enriches the standard JSON response of Message, providing additional information and fields, such as the linked-to and blocks fields.