class KsqlExceptionMapper implements ExceptionMapper<Throwable> {

  @Override
  public Response toResponse(Throwable exception) {
    // TODO: Distinguish between exceptions that warrant a stack trace and ones that don't
    if (exception instanceof KsqlRestException) {
      KsqlRestException restException = (KsqlRestException)exception;
      return restException.getResponse();
    }
    if (exception instanceof WebApplicationException) {
      WebApplicationException webApplicationException = (WebApplicationException)exception;
      return Response
          .status(
              Response.Status.fromStatusCode(
                  webApplicationException.getResponse().getStatus()))
          .type(MediaType.APPLICATION_JSON_TYPE)
          .entity(
              new KsqlErrorMessage(
                  webApplicationException.getResponse().getStatus()
                      * Errors.HTTP_TO_ERROR_CODE_MULTIPLIER,
                  webApplicationException))
          .build();
    }
    return Response
        .status(Response.Status.INTERNAL_SERVER_ERROR)
        .type(MediaType.APPLICATION_JSON_TYPE)
        .entity(new KsqlErrorMessage(Errors.ERROR_CODE_SERVER_ERROR, exception))
        .build();
  }

}