ztkmkoo / dss

Asynchronous distributed server system (dss)
MIT License
19 stars 13 forks source link

Add ExceptionHandler interface that users can customize exception handler #115

Closed Alencion closed 4 years ago

Alencion commented 4 years ago

Currently, the users with dss cannot handle exceptions.

So, I suggest adding the exception handler.

public class Application{
    public static void main() throws InterruptedException {
        DssRestServer dssRestServer = new DssRestServer("127.0.0.1", 8181);
        dssRestServer
                .addDssService(new MyService())
                .addExceptionHandler(new GlobalExceptionHandler());

        dssRestServer.start();
    }
}

public class GlobalExceptionHandler implements DssServiceExceptionHandler {
    // to handle all NullpointerException
    @ExceptionHandler(exception = NullPointerException.class)
    public DssRestServiceResponse nullPointerExceptionHandler(DssRestServiceRequest request) {
        System.out.println("Global nullPointerExceptionHandler");

        return new Response("To handle nullPointerException");
    }

    // to handle IOException only on myservice
    @ServiceExceptionHandler(service = MyService.class, exception = IOException.class)
    public void serviceExceptionHandler() {
        System.out.println("IOException in my service");
    }
}

like this.

ztkmkoo commented 4 years ago

@Alencion I agree with you. Could you push the pr?

Alencion commented 4 years ago

Thanks @ztkmkoo

I'll try.