import com.jimi.framework.model.OrderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobaExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobaExceptionHandler.class);
@ExceptionHandler(Throwable.class)
@ResponseBody
public Map handleException(Throwable exception) {
Map map = new HashMap<>();
map.put("errorCode", 500);
map.put("errorMsg", exception.toString());
map.put("timestamp", System.currentTimeMillis());
log.error("Exception", exception);
return map;
}
@ExceptionHandler(OrderException.class)
@ResponseBody
public Map handleOrderException(OrderException orderException) {
Map map = new HashMap<>();
map.put("errorCode", orderException.getCode());
map.put("errorMsg", orderException.getMsg());
map.put("timestamp", System.currentTimeMillis());
log.error(orderException.toString());
return map;
}
}