自定义统一异常处理,规范了部分类名

This commit is contained in:
zayac 2023-09-09 19:45:09 +08:00
parent 811537d09c
commit f7d68b1dab
13 changed files with 249 additions and 42 deletions

1
.gitignore vendored
View File

@ -31,3 +31,4 @@ build/
### VS Code ###
.vscode/
/src/test/

View File

@ -1,23 +0,0 @@
package com.zayac.changeurl.api;
import com.zayac.changeurl.service.ChangeURLService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zayac
* @since 2023-08-30 12:07
*/
@RestController
@RequestMapping("/api/v1")
public class ChangeURLApi {
@Resource
private ChangeURLService changeURLService;
@GetMapping("/change")
public Boolean changeURL(String text) {
return changeURLService.change(text);
}
}

View File

@ -0,0 +1,29 @@
package com.zayac.changeurl.api;
import cn.hutool.core.util.StrUtil;
import com.zayac.changeurl.exception.BizException;
import com.zayac.changeurl.response.Result;
import com.zayac.changeurl.service.ChangeUrlService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zayac
* @since 2023-08-30 12:07
*/
@RestController
@RequestMapping("/api/v1")
public class ChangeUrlApi {
@Resource
private ChangeUrlService changeUrlService;
@GetMapping("/change")
public Result<Boolean> changeUrl(String text) {
if (StrUtil.isBlank(text)) {
throw new BizException(400, "发送信息不能为空");
}
return Result.success(changeUrlService.change(text));
}
}

View File

@ -1,10 +1,11 @@
package com.zayac.changeurl.entity;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.zayac.changeurl.annotation.MsgAnnotation;
import com.zayac.changeurl.common.Constant;
import com.zayac.changeurl.enums.ErrorEnum;
import com.zayac.changeurl.exception.BizException;
import lombok.Data;
import java.lang.reflect.Field;
@ -39,7 +40,9 @@ public class HthMsgEntity {
val = ReUtil.getGroup1(Constant.URL_PATTERN, array[i + 1]);
}
}
Assert.notBlank(val);
if (StrUtil.isBlank(val)) {
throw new BizException(ErrorEnum.MSG_ERROR);
}
field.setAccessible(true);
try {
field.set(this, val);

View File

@ -0,0 +1,39 @@
package com.zayac.changeurl.enums;
/**
* @author zayac
* @since 2023-09-09 18:01
*/
public enum ErrorEnum {
// 数据操作错误定义
SUCCESS(200, "nice"),
NO_PERMISSION(403, "你没得权限"),
NO_AUTH(401, "你能不能先登录一下"),
MSG_ERROR(400,"飞机信息转换错误"),
NOT_FOUND(404, "未找到该资源!"),
INTERNAL_SERVER_ERROR(500, "服务器跑路了"),
;
/**
* 错误码
*/
private final Integer code;
/**
* 错误信息
*/
private final String msg;
ErrorEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}

View File

@ -0,0 +1,44 @@
package com.zayac.changeurl.exception;
import com.zayac.changeurl.enums.ErrorEnum;
import java.io.Serial;
/**
* @author zayac
* @since 2023-09-09 17:44
*/
public class BizException extends RuntimeException{
@Serial
private static final long serialVersionUID = -6016636225858202925L;
/**
* 错误码
*/
protected Integer code;
/**
* 错误信息
*/
protected String msg;
public BizException(Integer code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public BizException(ErrorEnum error) {
super();
this.code = error.getCode();
this.msg = error.getMsg();
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}

View File

@ -0,0 +1,29 @@
package com.zayac.changeurl.exception;
import com.zayac.changeurl.response.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import static com.zayac.changeurl.enums.ErrorEnum.INTERNAL_SERVER_ERROR;
/**
* @author zayac
* @since 2023-09-09 17:36
*/
@ControllerAdvice
public class GlobalExceptionHandler{
@ExceptionHandler(Exception.class)
@ResponseBody
public <T> Result<T> handleAllException(Exception e) {
return Result.error(INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(BizException.class)
@ResponseBody
public <T> Result<T> handleBizException(Exception e) {
BizException bizException = (BizException) e;
return Result.error(bizException.getCode(), bizException.getMsg());
}
}

View File

@ -0,0 +1,45 @@
package com.zayac.changeurl.response;
import com.zayac.changeurl.enums.ErrorEnum;
import lombok.Data;
/**
* @author zayac
* @since 2023-09-09 17:59
*/
@Data
public class Result<T> {
//状态码
private Integer code;
//提示信息
private String msg;
//数据
private T data;
public Result() {
}
//自定义返回结果的构造方法
public Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> Result<T> success(T data) {
return new Result<>(200, "success", data);
}
public static <T> Result<T> success(String msg, T data) {
return new Result<>(200, msg, data);
}
public static <T> Result<T> error(ErrorEnum error) {
return new Result<>(error.getCode(), error.getMsg(), null);
}
public static <T> Result<T> error(int code, String msg) {
return new Result<>(code, msg, null);
}
}

View File

@ -4,6 +4,6 @@ package com.zayac.changeurl.service;
* @author zayac
* @since 2023-08-30 12:07
*/
public interface ChangeURLService {
public interface ChangeUrlService {
Boolean change(String text);
}

View File

@ -2,13 +2,17 @@ package com.zayac.changeurl.service;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.zayac.changeurl.common.Constant;
import com.zayac.changeurl.entity.*;
import com.zayac.changeurl.entity.HthMsgEntity;
import com.zayac.changeurl.entity.HthTemplateEntity;
import com.zayac.changeurl.entity.KyMsgEntity;
import com.zayac.changeurl.entity.KyTemplateEntity;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
@ -16,16 +20,13 @@ import java.util.*;
* @since 2023-08-30 12:08
*/
@Service
public class ChangeURLServiceImpl implements ChangeURLService {
public class ChangeUrlServiceImpl implements ChangeUrlService {
private static final String KYJS = "ky.js";
private static final String HTHJS = "hth.js";
@Override
public Boolean change(String text) {
//如果传入参数为空,返回修改失败
if (StrUtil.isEmpty(text)) {
return false;
}
String[] arr = text.split("\n");
//传入text 包含SEO 专用域名 则为开云
@ -71,12 +72,11 @@ public class ChangeURLServiceImpl implements ChangeURLService {
.replace(hthTemplate.getApp2(), hthMsg.getTyApp())
.replace(hthTemplate.getPc(), hthMsg.getPc())
.replace(hthTemplate.getH5(), hthMsg.getH5());
modifyFile(template, hthTemplate.getTargets());
List<File> files = modifyFile(template, hthTemplate.getTargets());
//读取修改过的文件 校验修改成功失败
String result = FileUtil.readUtf8String(HTHJS);
String result = FileUtil.readUtf8String(files.get(0));
//如果修改后的js文件中
return ReUtil.count(Constant.URL_PATTERN, result) >= 4;
}
}
@ -96,14 +96,16 @@ public class ChangeURLServiceImpl implements ChangeURLService {
}
}
public void modifyFile(String text, List<String> paths) {
public List<File> modifyFile(String text, List<String> paths) {
List<File> files = new ArrayList<>();
for (String target : paths) {
if (FileUtil.exist(target)) {
FileUtil.del(target);
} else {
FileUtil.newFile(target);
File file = FileUtil.newFile(target);
files.add(FileUtil.writeString(text, file, "UTF-8"));
}
FileUtil.writeString(text, target, "UTF-8");
}
return files;
}
}

View File

@ -0,0 +1,38 @@
msg:
ky:
startWith:
web: "WEB"
h5: "H5"
app: "全站"
hth:
startWith:
web: ""
h5: ""
app1: ""
app2: ""
js:
ky:
kyweb1: "kyPc1"
kyweb2: "kyPc2"
kyh51: "kyH5"
kyh52: ""
kyApp1: "kyApp"
kyApp2: ""
hth:
hthweb: ""
hthh5: ""
hthApp1: ""
hthApp2: ""
path:
template: "link.js.template"
targets: "link.js"
server:
port: 8888
spring:
cache:
type: CAFFEINE
caffeine:
spec: maximumSize=100,expireAfterWrite=10m
ip:
white: "127.0.0.1"

View File

@ -6,4 +6,4 @@ spring:
caffeine:
spec: maximumSize=100,expireAfterWrite=10m
ip:
white: "0.0.0.0"
white: "127.0.0.1"

View File

@ -1,4 +1,4 @@
const hthCode = '3016341',
const hthCode = '3016341'
var hth_link = {
hthApp: '{{hthApp}}/?i_code='+hthCode,