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

com.invms.x.controllers.ExceptionAdvice Maven / Gradle / Ivy

package com.invms.x.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.invms.x.components.DefaultException;
import com.invms.x.models.DataModel;
import com.invms.x.utils.ComUtils;

/**
 * 异常通知器
 */
@RestControllerAdvice
public class ExceptionAdvice {
	/**
	 * 日志器
	 */
	private static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

	/**
	 * 全局异常处理
	 * 
	 * @param e
	 *            异常
	 * @return 返回结果
	 */
	@ExceptionHandler(Exception.class)
	public Object globalExceptionHandler(Exception e) {
		Throwable temp;
		Throwable throwable = e;
		while ((temp = throwable.getCause()) != null) {
			throwable = temp;
		}
		if (!(throwable instanceof DefaultException)) {
			logger.error("", e);
		}
		String message = throwable.getMessage();
		if (ComUtils.empty(message)) {
			message = throwable.toString();
		}
		return DataModel.createModel(message);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy