package com.firefly.example.reactive.coffee.store.router.impl.sys;

import com.firefly.$;
import com.firefly.annotation.Component;
import com.firefly.codec.http2.model.HttpStatus;
import com.firefly.example.reactive.coffee.store.vo.Response;
import com.firefly.server.http2.router.Handler;
import com.firefly.server.http2.router.RoutingContext;
import com.firefly.utils.log.slf4j.ext.LazyLogger;
import reactor.core.publisher.Mono;

import java.time.Duration;

/**
 * @author Pengtao Qiu
 */
@Component("globalHandler")
public class GlobalHandler implements Handler {

    private static final LazyLogger logger = LazyLogger.create();

    @Override
    public void handle(RoutingContext ctx) {
        ctx.getResponse().setAsynchronous(true);
        Mono.fromFuture(ctx.nextFuture())
            .timeout(Duration.ofSeconds(1))
            .subscribe(ret -> ctx.end(), ex -> {
                if (ex instanceof IllegalArgumentException) {
                    ctx.setStatus(HttpStatus.BAD_REQUEST_400);
                    logger.info(() -> $.string.replace("Request error. [{}], [{}]", ctx.getURI(), ex.getMessage()));
                    Response<Boolean> response = new Response<>();
                    response.setStatus(HttpStatus.BAD_REQUEST_400);
                    response.setMessage(ex.getMessage());
                    ctx.writeJson(response).end();
                } else {
                    ctx.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                    logger.error(() -> "Server exception. " + ctx.getURI(), ex);
                    Response<Boolean> response = new Response<>();
                    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                    response.setMessage("server exception, " + ex.getMessage());
                    ctx.writeJson(response).end();
                }
            });
    }
}

@@ -892,26 +892,8 @@
     
-if (ex instanceof 
 Ille
@@ -916,258 +916,77 @@
 tion
-) %7B%0A                    ctx.setStatus(HttpStatus.BAD_REQUEST_400);%0A                    logger.info(() -%3E $.string.replace(%22Request error. %5B%7B%7D%5D, %5B%7B%7D%5D%22, ctx.getURI(), ex.getMessage()));%0A                    Response%3CBoolean%3E respons
+ e = toIllegalArgumentException(ex);%0A                if (
 e 
+!
 = n
-ew Response%3C%3E();
+ull) %7B
 %0A   
@@ -1008,160 +1008,43 @@
   re
-sponse.setStatus(HttpStatus.BAD_REQUEST_400);%0A                    response.setMessage(ex.getMessage());%0A                    ctx.writeJson(response).end(
+nderIllegalArgumentException(ctx, e
 );%0A 
@@ -1516,16 +1516,16 @@
       %7D%0A
-
         
@@ -1537,11 +1537,845 @@
 );%0A    %7D
+%0A%0A    private IllegalArgumentException toIllegalArgumentException(Throwable ex) %7B%0A        if (ex instanceof IllegalArgumentException) %7B%0A            return (IllegalArgumentException) ex;%0A        %7D else if (ex.getCause() instanceof IllegalArgumentException) %7B%0A            return (IllegalArgumentException) ex.getCause();%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A    private void renderIllegalArgumentException(RoutingContext ctx, IllegalArgumentException ex) %7B%0A        ctx.setStatus(HttpStatus.BAD_REQUEST_400);%0A        logger.info(() -%3E $.string.replace(%22Request error. %5B%7B%7D%5D, %5B%7B%7D%5D%22, ctx.getURI(), ex.getMessage()));%0A        Response%3CBoolean%3E response = new Response%3C%3E();%0A        response.setStatus(HttpStatus.BAD_REQUEST_400);%0A        response.setMessage(ex.getMessage());%0A        ctx.writeJson(response).end();%0A    %7D
 %0A%7D%0A

clean up code