sczyh30 / vertx-blueprint-microservice

Vert.x Blueprint Project - Micro-Shop microservice application
Apache License 2.0
772 stars 301 forks source link

Exception Handling in the Service Implementations is unflexible #20

Open a-marcel opened 7 years ago

a-marcel commented 7 years ago

Hi,

the idea with the Rest API Class and the Service Impl is very good and i like it. My Problem is, that if an Impl. throws an exception, it results all the time in a HTTP Error 500.

From my point of view, there should be some default Exceptions.

An idea is to create a BaseException and some Special Exception (extends the BaseException) and containing the http error code.

Inside the Service Impl i can throw this special Exception and the function internalError handle this and change the HTTP Status.

protected void internalError(RoutingContext context, Throwable ex) {
        context.response().setStatusCode(500).putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json");

        if (ex instanceof CustomBaseException) {
            context.response().setStatusCode(((CustomBaseException)ex).getHttpStatusCode());
        }

        context.response().end(new JsonObject().put("error", ex.getMessage()).encodePrettily());
    }

Or do you've an other concept to control the HTTP Status codes inside the Service Implementations ?

Thanks Marcel