Error mapping
The process of error mapping consists of two steps:
- transformation from Throwable to ServiceMessage (server-side error mapping)
- transformation from ServiceMessage to Throwable (client-side error mapping)
If no custom error mappers are provided the DefaultErrorMapper (which is default scalecube implementation) will be used.
Server-side error mapping
For server-side error mapping below interface should be implemented:
@FunctionalInterface public interface ServiceProviderErrorMapper { /** * Maps an exception to a {@link ServiceMessage}. * * @param throwable the exception to map to a service message. * @return a service message mapped from the supplied exception. */ ServiceMessage toMessage(Throwable throwable); }
To enable custom implementation of ServiceProviderErrorMapper it should be passed to Microservices builder. There is a possibility to declare default mapper for the whole Microservices instance (which means that every service defined in this Microservices will use this mapper) or per service instance which are defined in Microservices.
Microservices ms = Microservices.builder() // default mapper for whole node .defaultErrorMapper(new ServiceAProviderErrorMapper()) .services(new ServiceAImpl()) .startAwait();
Microservices ms = Microservices.builder() .services( ServiceInfo.fromServiceInstance(new ServiceAImpl()) // mapper per service instance .errorMapper(new ServiceAProviderErrorMapper()) .build()) .startAwait();
Client-side error mapping
For client-side error mapping below interface should be implemented:
@FunctionalInterface public interface ServiceClientErrorMapper { /** * Maps service message to an exception. * * @param message the message to map to an exception. * @return an exception mapped from qualifier and error data. */ Throwable toError(ServiceMessage message); }
To enable custom implementation of ServiceClientErrorMapper it should be passed to ServiceCall builder.
ms.call() .errorMapper(new ServiceAClientErrorMapper()) .create() .api(ServiceA.class) .doStuff() .subscribe();