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

org.zodiac.autoconfigure.log.error.ReactiveErrorHandler Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.log.error;

import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.zodiac.commons.util.Colls;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ReactiveErrorHandler extends DefaultErrorWebExceptionHandler {

    private static final Map SERIES_VIEWS;

    static {
        Map views = new EnumMap<>(HttpStatus.Series.class);
        views.put(HttpStatus.Series.CLIENT_ERROR, "4xx");
        views.put(HttpStatus.Series.SERVER_ERROR, "5xx");
        SERIES_VIEWS = Collections.unmodifiableMap(views);
    }

    private final ErrorProperties errorProperties;

    public ReactiveErrorHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
        ErrorProperties errorProperties, ApplicationContext applicationContext) {
        super(errorAttributes, resourceProperties, errorProperties, applicationContext);
        this.errorProperties = errorProperties;
    }

    @Override
    protected Mono renderErrorView(ServerRequest request) {
        boolean includeStackTrace = isIncludeStackTrace(request, MediaType.ALL);
        Map error = getErrorAttributes(request, includeStackTrace);
        int errorStatus = getHttpStatus(error);
        ServerResponse.BodyBuilder responseBody = ServerResponse.status(errorStatus).contentType(MediaType.APPLICATION_JSON);
        return Flux.just(getData(errorStatus).toArray(new String[] {}))
                .flatMap((viewName) -> renderErrorView(viewName, responseBody, error))
                .switchIfEmpty(this.errorProperties.getWhitelabel().isEnabled()
                        ? renderDefaultErrorView(responseBody, error) : Mono.error(getError(request)))
                .next();
    }

    @Override
    protected Mono renderErrorResponse(ServerRequest request) {
        return super.renderErrorResponse(request);
    }

    private List getData(int errorStatus) {
        List data = Colls.list();
        data.add("error/" + errorStatus);
        HttpStatus.Series series = HttpStatus.Series.resolve(errorStatus);
        if (series != null) {
            data.add("error/" + SERIES_VIEWS.get(series));
        }
        data.add("error/error");
        return data;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy