ExceptionHandlerBaseRoute.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package com.sagacloud.route;
  2. /*
  3. * Author: Jxing
  4. * Create Time: 2018/7/12
  5. */
  6. import com.sagacloud.Exceptions.InvalidPostException;
  7. import com.sagacloud.utils.VendersUtil;
  8. import org.apache.camel.Exchange;
  9. import org.apache.camel.builder.RouteBuilder;
  10. import org.apache.camel.http.common.HttpOperationFailedException;
  11. public class ExceptionHandlerBaseRoute extends RouteBuilder {
  12. @Override
  13. public void configure() throws Exception {
  14. }
  15. protected void configExceptionHandler(){
  16. onException(InvalidPostException.class).handled(true).process(new org.apache.camel.Processor() {
  17. @Override
  18. public void process(Exchange exchange) throws Exception {
  19. Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
  20. exchange.getOut().setBody(VendersUtil.errorJsonMsg(exception.getMessage()));
  21. }
  22. });
  23. onException(HttpOperationFailedException.class).handled(true).process(new org.apache.camel.Processor() {
  24. @Override
  25. public void process(Exchange exchange) throws Exception {
  26. Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
  27. exchange.getOut().setBody(VendersUtil.errorJsonMsg("连接异常"));
  28. }
  29. });
  30. }
  31. }