@Override public ServiceMessage toMessage(Throwable throwable) { int errorCode = DEFAULT_ERROR_CODE; int errorType = DEFAULT_ERROR_CODE; if (throwable instanceof ServiceException) { errorCode = ((ServiceException) throwable).errorCode(); if (throwable instanceof BadRequestException) { errorType = BadRequestException.ERROR_TYPE; } else if (throwable instanceof UnauthorizedException) { errorType = UnauthorizedException.ERROR_TYPE; } else if (throwable instanceof ServiceUnavailableException) { errorType = ServiceUnavailableException.ERROR_TYPE; } else if (throwable instanceof InternalServiceException) { errorType = InternalServiceException.ERROR_TYPE; } } String errorMessage = Optional.ofNullable(throwable.getMessage()).orElseGet(throwable::toString); return ServiceMessage.error(errorType, errorCode, errorMessage); } }
@Override public ServiceMessage toMessage(Throwable throwable) { // implement service mapping logic if (throwable instanceof ServiceAException) { ServiceAException e = (ServiceAException) throwable; return ServiceMessage.error(BadRequestException.ERROR_TYPE, e.code(), e.getMessage()); } // or delegate it to default mapper return DefaultErrorMapper.INSTANCE.toMessage(throwable); } }