增加异常处理,和返回模板

This commit is contained in:
2025-07-09 23:34:23 +08:00
parent 2732fa82db
commit 40b2b1121f
7 changed files with 383 additions and 23 deletions

View File

@ -0,0 +1,12 @@
package com.sczx.store.common;
/**
* @Author: 张黎
* @Date: 2025/07/09/20:20
* @Description:
*/
public interface IApiCode {
String getCode();
String getMsg();
}

View File

@ -0,0 +1,122 @@
package com.sczx.store.common;
import com.sczx.store.common.enums.ApiErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: 张黎
* @Date: 2025/07/09/20:25
* @Description:
*/
@ApiModel(value = "公共返回结果")
@Builder
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1331134667810352183L;
@ApiModelProperty(value = "成功标识")
private boolean success;
@ApiModelProperty(value = "返回编码")
private String code;
@ApiModelProperty(value = "返回信息说明")
private String msg;
private String issue;
@ApiModelProperty(value = "返回数据")
private T data;
public Result(boolean success, String code, String msg, String issue, T data) {
this.success = success;
this.code = code;
this.msg = msg;
this.issue = issue;
this.data = data;
}
public static Result result(IApiCode apiCode) {
return result(apiCode, (Object)null);
}
public static Result result(IApiCode apiCode, Object data) {
return result(apiCode, apiCode.getMsg(), data);
}
public static Result result(IApiCode apiCode, String issue, Object data) {
return result((IApiCode)apiCode, (String)null, issue, data);
}
public static Result result(IApiCode apiCode, String msg, String issue, Object data) {
String message = apiCode.getMsg();
if (StringUtils.isNotBlank(msg)) {
message = msg;
}
return result(apiCode.getCode(), message, issue, data);
}
public static Result result(String code, String msg, String issue, Object data) {
return builder().code(code).success("0".equals(code)).msg(msg).issue(issue).data(data).build();
}
public static Result ok() {
return ok((Object)null);
}
public static Result ok(Object data) {
return result(ApiErrorCode.SUCCESS, data);
}
public static Result ok(String key, Object value) {
Map<String, Object> map = new HashMap(1);
map.put(key, value);
return ok(map);
}
public static Result fail(String msg) {
return result((IApiCode)ApiErrorCode.FAIL, msg, msg, (Object)null);
}
public static Result fail(IApiCode apiCode) {
return result(apiCode, (Object)null);
}
public static Result fail(IApiCode apiCode, String issue) {
return result(apiCode, issue, (Object)null);
}
public static Result fail(String code, String msg) {
return result((String)code, msg, msg, (Object)null);
}
public static Result fail(String code, String msg, String issue) {
return result((String)code, msg, issue, (Object)null);
}
public static Result fail(String code, String msg, String issue, Object data) {
return result(code, msg, issue, data);
}
public static Result fail(IApiCode apiCode, String issue, Object data) {
if (ApiErrorCode.SUCCESS == apiCode) {
throw new RuntimeException("失败结果状态码不能为" + ApiErrorCode.SUCCESS.getCode());
} else {
return result(apiCode, issue, data);
}
}
public static Result fail(String key, Object value) {
Map<String, Object> map = new HashMap();
map.put(key, value);
return result(ApiErrorCode.FAIL, map);
}
public Result() {
}
}

View File

@ -0,0 +1,55 @@
package com.sczx.store.common.enums;
import com.sczx.store.common.IApiCode;
/**
* @Author: 张黎
* @Date: 2025/07/09/20:22
* @Description:
*/
public enum ApiErrorCode implements IApiCode {
SUCCESS("0", "操作成功"),
ARG_ERROR("401000", "参数错误"),
NOT_PERMISSION("401001", "没有权限"),
HTTP_MEDIA_TYPE_NOT_SUPPORTED_ERROR("401002", "media类型出错"),
HTTP_METHOD_NOT_ALLOW_ERROR("401003", "http请求method错误"),
BODY_NOT_MATCH("401004", "请求的数据格式不符!"),
NOT_FOUND("404000", "你请求的路径不存在"),
UNAUTHORIZED("404001", "非法访问"),
FAIL("500000", "操作失败"),
INNER_ERROR("500001", "服务器内部异常");
private final String code;
private final String msg;
private ApiErrorCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
public static ApiErrorCode getApiCode(String code) {
ApiErrorCode[] ecs = values();
ApiErrorCode[] var2 = ecs;
int var3 = ecs.length;
for(int var4 = 0; var4 < var3; ++var4) {
ApiErrorCode ec = var2[var4];
if (ec.getCode().equals(code)) {
return ec;
}
}
return SUCCESS;
}
@Override
public String getCode() {
return this.code;
}
@Override
public String getMsg() {
return this.msg;
}
}

View File

@ -2,17 +2,15 @@ package com.sczx.store.controller;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sczx.store.common.Result;
import com.sczx.store.dto.CompanyStoreDTO; import com.sczx.store.dto.CompanyStoreDTO;
import com.sczx.store.dto.req.StoreDistanceReq; import com.sczx.store.dto.req.StoreDistanceReq;
import com.sczx.store.service.StoreService; import com.sczx.store.service.StoreService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* <p> * <p>
* 门店 前端控制器 * 门店 前端控制器
@ -31,7 +29,7 @@ public class StoreClientController {
@ApiOperation(value = "根据经纬度查询门店信息") @ApiOperation(value = "根据经纬度查询门店信息")
@PostMapping("/listStoresByBBoxWithDistance") @PostMapping("/listStoresByBBoxWithDistance")
ResponseEntity<IPage<CompanyStoreDTO>> listStoresByBBoxWithDistance(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody StoreDistanceReq storeDistanceReq){ Result<IPage<CompanyStoreDTO>> listStoresByBBoxWithDistance(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody StoreDistanceReq storeDistanceReq){
return ResponseEntity.ok(storeService.listStoresByBBoxWithDistance(storeDistanceReq, pageNo, pageSize)); return Result.ok(storeService.listStoresByBBoxWithDistance(storeDistanceReq, pageNo, pageSize));
} }
} }

View File

@ -0,0 +1,85 @@
package com.sczx.store.exception;
import com.sczx.store.common.IApiCode;
import com.sczx.store.common.enums.ApiErrorCode;
/**
*
* @author zhangli
*/
public class BizException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public BizException() {
super();
}
public BizException(IApiCode apiCode) {
super(apiCode.getCode());
this.errorCode = apiCode.getCode();
this.errorMsg = apiCode.getMsg();
}
public BizException(IApiCode apiCode, Throwable cause) {
super(apiCode.getCode(), cause);
this.errorCode = apiCode.getCode();
this.errorMsg = apiCode.getMsg();
}
public BizException(String errorMsg) {
super(errorMsg);
this.errorCode = ApiErrorCode.FAIL.getCode();
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
@Override
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}

View File

@ -1,28 +1,17 @@
package com.sczx.store.exception; package com.sczx.store.exception;
import com.sczx.store.common.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/** /**
* 应用模块名称<p> * @author zhangli
* <p>
* 代码描述<p>
* <p>
* Copyright: Copyright (C) 2021 CD Finance Management Co., Ltd. All rights reserved. <p>
* <p>
* Company: 中和农信项目管理有限公司<p>
*
* @author zhonghui
* @since 2021/9/13 2:46 PM
*/ */
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
@ -36,12 +25,25 @@ public class GlobalExceptionHandler {
* @param e * @param e
* @return * @return
*/ */
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = BizException.class)
@ResponseBody @ResponseBody
public ResponseEntity bizExceptionHandler(HttpServletRequest req, Exception e) { public Result bizExceptionHandler(HttpServletRequest req, BizException e) {
log.error("服务器异常!原因是:{}",e.getMessage(), e); log.warn("发生业务异常!原因是:{}", e.getErrorMsg(), e);
return ResponseEntity.noContent().build(); return Result.fail(e.getErrorCode(), e.getErrorMsg());
} }
@ExceptionHandler(value = InnerException.class)
@ResponseBody
public Result innerExceptionHandler(HttpServletRequest req, InnerException e) {
log.error("发生服务内部异常!原因是:{}", e.getErrorMsg(), e);
return Result.fail(e.getErrorCode(), e.getErrorMsg());
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result exceptionHandler(HttpServletRequest req, Exception e) {
log.error("意料外异常!原因是:{}", e.getMessage(), e);
return Result.fail("99999", "系统内部异常");
}
} }

View File

@ -0,0 +1,86 @@
package com.sczx.store.exception;
import com.sczx.store.common.IApiCode;
import com.sczx.store.common.enums.ApiErrorCode;
/**
*
* @author zhangli
*/
public class InnerException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public InnerException() {
super();
}
public InnerException(IApiCode apiCode) {
super(apiCode.getCode());
this.errorCode = apiCode.getCode();
this.errorMsg = apiCode.getMsg();
}
public InnerException(IApiCode apiCode, Throwable cause) {
super(apiCode.getCode(), cause);
this.errorCode = apiCode.getCode();
this.errorMsg = apiCode.getMsg();
}
public InnerException(String errorMsg) {
super(errorMsg);
this.errorCode = ApiErrorCode.INNER_ERROR.getCode();
this.errorMsg = errorMsg;
}
public InnerException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public InnerException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
@Override
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}