package com.winsun.advice;

import com.winsun.auth.core.common.model.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *
 * @date 2022-03-15 11:08
 */
@Slf4j
@RestControllerAdvice
@Order(-2)
public class ErrorControllerAdvice {

    private final static Integer errorLength = 20;

    @ExceptionHandler(RuntimeException.class)
    public ResponseData<String> allErrorHandler(Exception e) {
        String message = e.getMessage();
        if (StringUtils.isNotBlank(message) && message.length() > errorLength) {
            return ResponseData.error("系统异常！");
        }else {
            return ResponseData.error(e.getMessage());
        }
    }
}
