class AbstractEdgeDispatcher extends AbstractVertxHttpDispatcher {

  protected void onFailure(RoutingContext context) {
    LOGGER.error("edge server failed.", context.failure());
    HttpServerResponse response = context.response();
    if (response.closed() || response.ended()) {
      return;
    }

    if (context.failure() instanceof InvocationException) {
      InvocationException exception = (InvocationException) context.failure();
      response.setStatusCode(exception.getStatusCode());
      response.setStatusMessage(exception.getReasonPhrase());
      if (null == exception.getErrorData()) {
        response.end();
        return;
      }

      String responseBody;
      try {
        responseBody = RestObjectMapperFactory.getRestObjectMapper().writeValueAsString(exception.getErrorData());
        response.putHeader("Content-Type", MediaType.APPLICATION_JSON);
      } catch (JsonProcessingException e) {
        responseBody = exception.getErrorData().toString();
        response.putHeader("Content-Type", MediaType.TEXT_PLAIN);
      }
      response.end(responseBody);
    } else {
      response.setStatusCode(Status.BAD_GATEWAY.getStatusCode());
      response.setStatusMessage(Status.BAD_GATEWAY.getReasonPhrase());
      response.end();
    }
  }

}