1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 /** 7 * 最基础的异常类。 8 * @author zhangxin13 9 * @version 2017年11月2日下午7:20:46 10 * @since 2017年11月2日 11 */ 12 abstract class AbstractException extends RuntimeException implements ExceptionInfo { 13 private static final long serialVersionUID = 3289598099629324399L; 14 private int code; 15 private String message; 16 private Object body; 17 public AbstractException(ExceptionInfo info){ 18 this.code = info.getCode(); 19 this.message = info.getMessage(); 20 this.body = null; 21 } 22 public AbstractException(ExceptionInfo info, Object body){ 23 this.code = info.getCode(); 24 this.message = info.getMessage(); 25 this.body = body; 26 } 27 @Override 28 public String getMessage() { 29 return message; 30 } 31 @Override 32 public int getCode() { 33 return code; 34 } 35 public Object getBody() { 36 return body; 37 } 38 }
1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 /** 7 * 业务异常。比如输入错误的数据导致业务失败,逻辑进入到非主线分支,需要拒绝服务,让客户端提交正确的参数即可恢复。 8 * 因此当遇到这种异常的时候,其实业务逻辑是执行成功的,只不过进入了非主线分支而已<br> 9 * 当发生BusinessException时,客户端返回的http code总是200 10 * @author zhangxin13 11 * @version 2017年11月7日上午9:40:35 12 * @since 2017年11月7日 13 */ 14 public abstract class BusinessException extends AbstractException { 15 private static final long serialVersionUID = -4853537210488828688L; 16 public BusinessException(ExceptionInfo info) { 17 super(info); 18 } 19 public BusinessException(ExceptionInfo info, Object body) { 20 super(info, body); 21 } 22 }
1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 /** 7 * 调用方异常。这类错误一般是servlet容器处理的,这里作为预留<br> 8 * 当发生ClientException时,客户端返回的http code默认400 9 * @author zhangxin13 10 * @version 2017年11月2日下午7:19:12 11 * @since 2017年11月2日 12 */ 13 public abstract class ClientException extends AbstractException { 14 private static final long serialVersionUID = 5214893309908224949L; 15 public ClientException(ExceptionInfo info) { 16 super(info); 17 } 18 public ClientException(ExceptionInfo info, Object body) { 19 super(info, body); 20 } 21 }
1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 /** 7 * 由服务器上的逻辑漏洞或者其他什么原因导致的无法由客户端重新提交来纠正的错误, 8 * 比如服务器代码上写的不严谨在某个地方报了空指针异常,这种问题客户端无论提交多少次正确的参数都无法修正, 9 * 因此是属于服务器内部错误。<br> 10 * 当发生ServerException时,向客户端返回的http code为5xx。 11 * @author zhangxin13 12 * @version 2017年11月2日下午7:19:20 13 * @since 2017年11月2日 14 */ 15 public abstract class ServerException extends AbstractException { 16 private static final long serialVersionUID = 3117351252348483331L; 17 public ServerException(ExceptionInfo info) { 18 super(info); 19 } 20 public ServerException(ExceptionInfo info, Object body) { 21 super(info, body); 22 } 23 }
1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 /** 7 * 8 * @author zhangxin13 9 * @version 2017年11月6日下午9:02:52 10 * @since 2017年11月6日 11 */ 12 public interface ExceptionInfo { 13 public int getCode(); 14 public String getMessage(); 15 }
1 /** 2 * 3 */ 4 package com.hikvision.building.cloud.gaia.common.exception; 5 6 import java.util.Arrays; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.slf4j.Logger; 14 import org.slf4j.LoggerFactory; 15 import org.springframework.util.CollectionUtils; 16 import org.springframework.web.bind.annotation.ControllerAdvice; 17 import org.springframework.web.bind.annotation.ExceptionHandler; 18 import org.springframework.web.bind.annotation.ResponseBody; 19 20 import com.hikvision.building.cloud.gaia.common.structs.HttpResult; 21 22 /** 23 * 全局异常处理 24 * @author zhangxin13 25 * @version 2017年11月6日下午9:25:06 26 * @since 2017年11月6日 27 */ 28 @ControllerAdvice 29 public class ExceptionHandlerAdvice { 30 private Logger logger = LoggerFactory.getLogger(getClass()); 31 32 @ExceptionHandler(BusinessException.class) 33 @ResponseBody 34 public HttpResult<String> handlerServerException(HttpServletRequest request, HttpServletResponse response, 35 BusinessException e){ 36 response.setStatus(200);//总是返回200 37 response.setContentType("application/json"); 38 response.setCharacterEncoding("UTF-8"); 39 response.setHeader("Cache-Control", "no-cache"); 40 logger.info("业务异常: code: {}, message: {}", e.getCode(), e.getMessage()); 41 logger.info("URI: {}, Method: {}, Parameter: {}, DebugMessage: {}", 42 request.getRequestURI(), request.getMethod(), parameterMap2String(request.getParameterMap()), 43 e.getBody()); 44 return build(e.getCode(), e.getMessage()); 45 } 46 @ExceptionHandler(ServerException.class) 47 @ResponseBody 48 public HttpResult<String> handlerServerException(HttpServletRequest request, HttpServletResponse response, 49 ServerException e) { 50 if(e.getCode()<=500 || e.getCode()>599){//没有设定 RFC 2616 规范所定义的错误码,则强制设定500 51 response.setStatus(500); 52 logger.warn("URI: {}, Method: {} 中返回了一个不严谨的错误码:{}", 53 request.getRequestURI(), request.getMethod(), e.getCode()); 54 }else{ 55 response.setStatus(e.getCode()); 56 } 57 response.setContentType("application/json"); 58 response.setCharacterEncoding("UTF-8"); 59 response.setHeader("Cache-Control", "no-cache"); 60 logger.warn("服务端异常", e); 61 logger.warn("URI: {}, Method: {}, Parameter: {}, DebugMessage: {}", 62 request.getRequestURI(), request.getMethod(), parameterMap2String(request.getParameterMap()), 63 e.getBody()); 64 return build(e.getCode(), e.getMessage()); 65 } 66 67 @ExceptionHandler(ClientException.class) 68 @ResponseBody 69 public HttpResult<String> handlerClientException(HttpServletRequest request, HttpServletResponse response, 70 ClientException e) { 71 if(e.getCode()<=400 || e.getCode()>499){//没有设定 RFC 2616 规范所定义的错误码,则强制设定400 72 response.setStatus(400); 73 logger.warn("URI: {}, Method: {} 中返回了一个不严谨的错误码:{}", 74 request.getRequestURI(), request.getMethod(), e.getCode()); 75 }else{ 76 response.setStatus(e.getCode()); 77 } 78 response.setContentType("application/json"); 79 response.setCharacterEncoding("UTF-8"); 80 response.setHeader("Cache-Control", "no-cache"); 81 logger.warn("客户端异常", e); 82 logger.warn("URI: {}, Method: {}, Parameter: {}, DebugMessage: {}", 83 request.getRequestURI(), request.getMethod(), parameterMap2String(request.getParameterMap()), 84 e.getBody()); 85 return build(e.getCode(), e.getMessage()); 86 } 87 88 @ExceptionHandler(Exception.class) 89 @ResponseBody 90 public HttpResult<String> handlerException(HttpServletRequest request, HttpServletResponse response, 91 Exception e) { 92 response.setStatus(500);//所有未捕获的的异常 93 response.setContentType("application/json"); 94 response.setCharacterEncoding("UTF-8"); 95 response.setHeader("Cache-Control", "no-cache"); 96 logger.error("发生未捕获异常! URI: {}, Method: {}, RemoteHost: {}", 97 request.getRequestURI(), request.getMethod(), request.getRemoteHost()); 98 logger.error("未捕获异常", e); 99 return build(500, e.getMessage()); 100 } 101 102 private String parameterMap2String(Map<String, String[]> map){ 103 if(CollectionUtils.isEmpty(map)){ 104 return "{}"; 105 } 106 StringBuilder sb = new StringBuilder("{"); 107 for(Entry<String, String[]> entry: map.entrySet()){ 108 sb.append(entry.getKey()) 109 .append('=') 110 .append(Arrays.toString(entry.getValue())) 111 .append(','); 112 } 113 sb.deleteCharAt(sb.length()-1).append('}'); 114 return sb.toString(); 115 } 116 117 private HttpResult<String> build(int code,String message){ 118 HttpResult<String> r = new HttpResult<>(); 119 r.setCode(code); 120 r.setMessage(message); 121 return r; 122 } 123 124 }
1 package com.hikvision.building.cloud.gaia.common.structs; 2 3 import io.swagger.annotations.ApiModel; 4 import io.swagger.annotations.ApiModelProperty; 5 6 @ApiModel 7 public class HttpResult<T> { 8 public static final int OK = 0; 9 @ApiModelProperty(value="消息码",required=true,example="0") 10 private Integer code = 0; 11 @ApiModelProperty(value="提示信息。主要用于开发调试,不建议显示给用户",required=true) 12 private String message; 13 @ApiModelProperty(value="返回包体",required=true) 14 private T data; 15 @ApiModelProperty(value="是否正常返回",required=true,example="true",notes="主要预留给某些不方便判断code的场景,只要code不是0,这个字段就是false") 16 private boolean success = true; 17 18 public HttpResult() {} 19 public HttpResult(T data) { 20 this.data = data; 21 } 22 23 public Integer getCode() { 24 return code; 25 } 26 27 public void setCode(Integer code) { 28 this.code = code; 29 if (code != OK) { 30 this.success = false; 31 } 32 } 33 34 public String getMessage() { 35 return message; 36 } 37 38 public void setMessage(String message) { 39 this.message = message; 40 } 41 42 public T getData() { 43 return data; 44 } 45 46 public void setData(T data) { 47 this.data = data; 48 } 49 50 public boolean isSuccess() { 51 return this.success; 52 } 53 54 @Override 55 public String toString() { 56 return "HttpResult [code=" + code + ", message=" + message + ", data=" + data + ", success=" + success + "]"; 57 } 58 59 }
1 package com.hikvision.building.cloud.neptune.community.biz.exception; 2 3 import com.hikvision.building.cloud.gaia.common.exception.BusinessException; 4 import com.hikvision.building.cloud.neptune.community.biz.constant.CommunityExceptionEnum; 5 6 public abstract class CommunityBusinessException extends BusinessException{ 7 private static final long serialVersionUID = 1979830642167998428L; 8 9 public CommunityBusinessException(CommunityExceptionEnum info,Object object) { 10 super(info,object); 11 } 12 13 public CommunityBusinessException(CommunityExceptionEnum info) { 14 super(info); 15 } 16 17 }
1 package com.hikvision.building.cloud.neptune.community.biz.exception; 2 3 import com.hikvision.building.cloud.neptune.community.biz.constant.CommunityExceptionEnum; 4 5 public class AreaNotExistException extends CommunityBusinessException{ 6 7 private static final long serialVersionUID = 6751736070431967875L; 8 9 public AreaNotExistException(Object object) { 10 super(CommunityExceptionEnum.AREA_NOT_EXIST,object); 11 } 12 13 }
1 package com.hikvision.building.cloud.neptune.device.biz.constant; 2 3 import com.hikvision.building.cloud.gaia.common.exception.ExceptionInfo; 4 5 /** 6 * @Description 异常码枚举类 7 * @author like15 8 * @version v1.0 9 * @date 2017年11月16日 下午1:35:02 10 * @modificationHistory=========================逻辑或功能性重大变更记录 11 * @modify by user: like15 2017年11月16日 12 * @since 2017年11月16日 13 */ 14 public enum DeviceExceptionEnum implements ExceptionInfo{ 15 16 /**pageNo或者pageSize信息为空。*/ 17 DEVICE_PAGE_PARAM_EXCEPTION(960001,"pageNo或者pageSize信息不合法,必须为正整数。"), 18 19 /**该社区下未配置网关信息。*/ 20 DEVICE_USERID_TOKEN_NULL(960002,"userId或者token信息为空。"), 21 22 /**该社区下未配置网关信息。*/ 23 DEVICE_QUERY_TENANTID_NULL(960003,"查询租户id失败。"), 24 25 /**该社区下未配置网关信息。*/ 26 GATEWAY_NOT_LINK(960004,"设备网关不存在或未连接"), 27 28 /**该社区下未配置网关信息。*/ 29 DEVICE_UNIT_ALREADY_EXIST(960005,"添加设备失败,该单元已存在其他门口机设备。"), 30 31 /**设备添加参数异常,devAddId、valiCode、ip、port、userName或者password为空。*/ 32 BAD_PARAMETER_DEVICE_ADD(960101,"设备添加参数异常,devAddId、ip、port、userName或者password为空。"), 33 34 /**该社区下未配置网关信息。*/ 35 DEVICE_GATEWAY_NOT_EXIST(960102,"该社区下未配置网关信息。"), 36 37 /**设备信息不存在或已删除。*/ 38 DEVICE_NOT_EXIST(960103,"设备信息不存在或已删除。"), 39 40 /**设备位置信息path查询异常。*/ 41 DEVICE_ADDRESS_PATH_EXCEPTION(960104,"设备位置信息path查询异常。"), 42 43 /**设备位置信息pathMap查询异常。*/ 44 DEVICE_ADDRESS_PATH_MAP_EXCEPTION(960105,"设备位置信息pathMap查询异常。"), 45 46 /**该设备已被添加。*/ 47 DEVICE_ALREADY_EXIST_EXCEPTION(960106,"该设备已被添加。"), 48 49 /**调用萤石协议删除设备失败。*/ 50 DEVICE_EZVIZ_DELETE_FAIL(960107,"调用萤石协议删除设备失败。"), 51 52 /**设备IP或者端口不正确。*/ 53 DEVICE_IP_POER_ILLEGAL(960108,"设备IP或者端口不正确。"), 54 55 /**设备用户名或密码不正确。*/ 56 DEVICE_USERNAME_PASSWORD_ILLEGAL(960109,"设备用户名或密码不正确。"), 57 58 /**设备用户名或密码不正确。*/ 59 DEVICE_VALICODE_ILLEGAL(960110,"设备验证码不正确。"), 60 61 /**设备位置信息path查询异常。*/ 62 DEVICE_ADDRESS_PATH_NUMBER_EXCEPTION(960111,"设备位置信息path编号查询异常。"), 63 64 /**通行权限路径下没有设备信息。*/ 65 PATH_DEVICE_NOT_EXIST(960112,"通行权限路径下没有设备信息。"), 66 67 /**卡号不能为空。*/ 68 CARD_BUMBER_NOT_EXIST(960113,"获取卡号信息异常。"), 69 70 /**调用萤石协议删除设备失败。*/ 71 DEVICE_DELETE_FAIL(960114,"删除失败。"), 72 73 /**communityId为空。*/ 74 BAD_PARAMETER_DEVICE_COMMUNITYID_NULL(960201,"communityId为空。"), 75 76 /**devId为空。*/ 77 BAD_PARAMETER_DEVICE_DEVSERIAL_NULL(960202,"devId为空。"), 78 79 /**communityId或者tenantId为空。*/ 80 BAD_PARAMETER_DEVICE_COMMUNITYID_TENANTID_NULL(960203,"communityId或者tenantId为空。"), 81 82 /**callId、callDuration或者eventTime为空。*/ 83 BAD_PARAMETER_CALL_NULL(960301,"callId、callDuration、devSerial或者eventTime为空。"), 84 85 /**callId对应的呼叫记录不存在为空。*/ 86 CALL_LOG_NOT_EXIST_EXCEPTION(960302,"callId对应的呼叫记录不存在为空。"), 87 ; 88 89 private int code; 90 private String message; 91 private DeviceExceptionEnum(int code, String message) { 92 this.code = code; 93 this.message = message; 94 } 95 @Override 96 public int getCode() { 97 return code; 98 } 99 100 @Override 101 public String getMessage() { 102 return message; 103 } 104 105 }