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

com.gitee.fufu669.config.exception.CacheServerExceptionHandler Maven / Gradle / Ivy

There is a newer version: 6.666.66021
Show newest version
package com.gitee.fufu669.config.exception;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.fasterxml.jackson.core.JsonGenerator;


@SuppressWarnings({ "rawtypes" })
/** @author wangfupeng */
public class CacheServerExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(CacheServerExceptionHandler.class);

	@ResponseBody
	@ExceptionHandler(CacheServerException.class)
	public ModelAndView serverExceptionHandler(RuntimeException e, HttpServletResponse response) {

		ModelAndView mav = new ModelAndView();
		if (e instanceof CacheServerException) {
			String status = ((CacheServerException) e).getCode();
			Jackson2JsonView view = new Jackson2JsonView();
			Map attributes = new HashMap<>(16);
			attributes.put("code", status);
			attributes.put("msg", e.getMessage());
			attributes.put("data", new CacheEmptyInstance());
			attributes.put("more info", "");
			view.setAttributesMap(attributes);
			view.setPrettyPrint(true);
			mav.setView(view);
			logger.info("【Error】", e);
		}
		return mav;
	}

	@ResponseBody
	@ExceptionHandler(BindException.class)
	public ModelAndView bindExceptionHandler(BindException e, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		Jackson2JsonView view = new Jackson2JsonView();
		Map attributes = new HashMap<>(16);
		attributes.put("code", CacheServerErrorCode.EXCEPTIONHANDLER_PARAM_ERROR);
		attributes.put("msg", "参数有误");
		attributes.put("data", new CacheEmptyInstance());
		List errors = e.getBindingResult().getAllErrors();
		StringBuilder moreInfo = new StringBuilder();
		if (!CollectionUtils.isEmpty(errors)) {
			errors.stream().filter(error -> error instanceof FieldError).forEach(error -> moreInfo
					.append(((FieldError) error).getField()).append(":").append(error.getDefaultMessage()).append(";"));
		}
		attributes.put("more info", moreInfo.toString());
		view.setAttributesMap(attributes);
		view.setPrettyPrint(true);
		mav.setView(view);
		logger.info("【Error】", e);
		return mav;
	}

	@ResponseBody
	@ExceptionHandler(Exception.class)
	public ModelAndView exceptionHandler(Exception e, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		Jackson2JsonView view = new Jackson2JsonView();
		Map attributes = new HashMap<>(16);
		if (e instanceof Exception) {
			attributes.put("code", CacheServerErrorCode.UNKNOWN);
			/* attributes.put("msg", "未知错误");*/
			/* .substring(0, 190) */
			attributes.put("msg", temp(e));
			/* attributes.put("msg", e.toString());*/
			attributes.put("data", new CacheEmptyInstance());
			attributes.put("more info", "");
			view.setAttributesMap(attributes);
			view.setPrettyPrint(true);
			mav.setView(view);
			logger.info("【Error】", e);
		}
		return mav;
	}

	private String temp(Exception e) {
		String aaa = e.getMessage();
		for (StackTraceElement element : e.getStackTrace()) {
			aaa += element.toString();
		}

		return aaa;
	}

	class Jackson2JsonView extends MappingJackson2JsonView {
		@Override
		protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
		}

		@Override
		protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy