All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.shacocloud.mirage.web.HandlerExceptionResolverComposite Maven / Gradle / Ivy

package cc.shacocloud.mirage.web;

import cc.shacocloud.mirage.utils.FutureUtils;
import cc.shacocloud.mirage.utils.SelfSortList;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.OrderUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 基于{@link HandlerExceptionResolver}实现的聚合类,它委托给其他的{@link HandlerExceptionResolver}执行
 */
public class HandlerExceptionResolverComposite implements HandlerExceptionResolver {

    private final List exceptionResolvers
            = new SelfSortList<>(n -> OrderUtils.getOrder(n.getClass(), Ordered.LOWEST_PRECEDENCE));

    /**
     * 设置要委托给的异常解析器列表
     */
    public void addExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) {
        addExceptionResolvers(Arrays.asList(exceptionResolvers));
    }

    /**
     * 设置要委托给的异常解析器列表
     */
    public void addExceptionResolvers(List exceptionResolvers) {
        this.exceptionResolvers.addAll(exceptionResolvers);
    }

    /**
     * 返回要委托的异常解析器列表
     */
    public List getExceptionResolvers() {
        return Collections.unmodifiableList(this.exceptionResolvers);
    }

    /**
     * 通过遍历已配置的异常解析器列表来解析异常
     */
    @Override
    public Future resolveException(HttpRequest request, HttpResponse response, VertxInvokeHandler handler, Throwable cause) {
        return FutureUtils.sequential(this.exceptionResolvers.iterator(),
                (resolvers, termination) -> resolvers.resolveException(request, response, handler, cause)
                        .onSuccess(res -> termination.set(res != null)));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy