CamelResponseHandler implements ExchangeHandler { @Override public void handleFault(final Exchange exchange) { final Object content = exchange.getMessage().getContent(); if (content instanceof Throwable) { try { mapContext(exchange, ExchangeMapper.getCamelMessage(_camelExchange, ExchangePhase.OUT)); } catch (Exception e) { SwitchYardCamelComponentLogger.ROOT_LOGGER.failedToMapSwitchYardContextPropertiesToCamel(e); } _camelExchange.setException((Throwable) content); return; } try { Message camelMsg = compose(exchange); camelMsg.setFault(true); } catch (Exception e) { _camelExchange.setException(e); } } CamelResponseHandler(final org.apache.camel.Exchange camelExchange, final ServiceReference reference, final MessageComposer<CamelBindingData> messageComposer); @Override void handleMessage(final Exchange switchYardExchange); @Override void handleFault(final Exchange exchange);  }
@Test public void fault() throws HandlerException { final String fault = "some fault"; final Exchange camelExchange = createCamelExchange(); final ServiceReference serviceReference = createMockServiceRef(); final org.switchyard.Exchange switchYardExchange = createMockExchangeWithBody(new MessageCreator() { @Override public Message create() { Message message = mock(Message.class); when(message.getContext()).thenReturn(new DefaultContext(Scope.MESSAGE)); when(message.getContent()).thenReturn(fault); return message; } }); final CamelResponseHandler responseHandler = new CamelResponseHandler(camelExchange, serviceReference, _messageComposer); responseHandler.handleFault(switchYardExchange); assertTrue(camelExchange.getIn().isFault()); assertSame(fault, camelExchange.getIn().getBody()); }
@Test public void exception() throws HandlerException { final IllegalArgumentException exception = new IllegalArgumentException(); final Exchange camelExchange = createCamelExchange(); final ServiceReference serviceReference = createMockServiceRef(); final org.switchyard.Exchange switchYardExchange = createMockExchangeWithBody(new MessageCreator() { @Override public Message create() { Message message = mock(Message.class); when(message.getContent()).thenReturn(exception); return message; } }); final CamelResponseHandler responseHandler = new CamelResponseHandler(camelExchange, serviceReference, _messageComposer); responseHandler.handleFault(switchYardExchange); assertSame(exception, camelExchange.getException()); }