package com.tgdz.uap.common.exception;
import com.tgdz.uap.common.base.vo.Result;
import com.tgdz.uap.common.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
public Result<Void> handleAuthenticationException(Exception e) {
log.error(ExceptionUtil.getMessage(e));
return Result.error(e.getMessage());
}
}
package com.tgdz.uap.common.util;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
@Slf4j
public class ExceptionUtil {
public static String getMessage(Exception e) {
String swStr = null;
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
pw.flush();
sw.flush();
swStr = sw.toString();
} catch (Exception ex) {
log.error(ex.getMessage());
}
return swStr;
}
}