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

org.babyfish.jimmer.spring.client.CodeBasedExceptionAdvice Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.spring.client;

import org.babyfish.jimmer.error.CodeBasedException;
import org.babyfish.jimmer.error.CodeBasedRuntimeException;
import org.babyfish.jimmer.spring.cfg.JimmerProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.*;

@ControllerAdvice
public class CodeBasedExceptionAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(CodeBasedExceptionAdvice.class);

    protected final JimmerProperties.ErrorTranslator errorTranslator;

    public CodeBasedExceptionAdvice(JimmerProperties properties) {
        this.errorTranslator = properties.getErrorTranslator();
        if (errorTranslator.isDebugInfoSupported()) {
            notice();
        }
    }

    @ExceptionHandler
    public ResponseEntity> handle(CodeBasedException ex) {
        LOGGER.error("Auto handled HTTP Error(" + CodeBasedException.class.getName() + ")", ex);
        return ResponseEntity
                .status(errorTranslator.getHttpStatus())
                .body(resultMap(ex));
    }

    @ExceptionHandler
    public ResponseEntity> handle(CodeBasedRuntimeException ex) {
        LOGGER.error("Auto handled HTTP Error(" + CodeBasedRuntimeException.class.getName() + ")", ex);
        return ResponseEntity
                .status(errorTranslator.getHttpStatus())
                .body(resultMap(ex));
    }

    protected void notice() {
        String builder = "\n" + "------------------------------------------------\n" +
                "|                                              |\n" +
                "|`jimmer.error-translator.debug-info-supported`|\n" +
                "|has been turned on, this is dangerous, please |\n" +
                "|make sure the current environment is          |\n" +
                "|NOT PRODUCTION!                               |\n" +
                "|                                              |\n" +
                "------------------------------------------------\n";
        LOGGER.info(builder);
    }

    protected Map resultMap(CodeBasedException ex) {
        Map resultMap = new LinkedHashMap<>();
        resultMap.put("family", ex.getFamily());
        resultMap.put("code", ex.getCode());
        resultMap.putAll(ex.getFields());
        if (errorTranslator.isDebugInfoSupported()) {
            resultMap.put("debugInfo", debugInfoMap(ex));
        }
        return resultMap;
    }

    protected Map resultMap(CodeBasedRuntimeException ex) {
        Map resultMap = new LinkedHashMap<>();
        resultMap.put("family", ex.getFamily());
        resultMap.put("code", ex.getCode());
        resultMap.putAll(ex.getFields());
        if (errorTranslator.isDebugInfoSupported()) {
            resultMap.put("debugInfo", debugInfoMap(ex));
        }
        return resultMap;
    }

    protected Map debugInfoMap(Throwable ex) {
        Map map = new LinkedHashMap<>();
        map.put("message", ex.getMessage());
        StackTraceElement[] elements = ex.getStackTrace();
        int size = Math.min(elements.length, errorTranslator.getDebugInfoMaxStackTraceCount());
        List stackFrames = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            stackFrames.add(elements[i].toString());
        }
        map.put("stackFrames", stackFrames);
        if (ex.getCause() != null) {
            map.put("causeBy", debugInfoMap(ex.getCause()));
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy