Skip to content

Commit

Permalink
异常定义、统一响应格式定义
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouGG committed Dec 3, 2024
1 parent b8dcb14 commit fc361e6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
43 changes: 43 additions & 0 deletions ChronAnt-type/src/main/java/cn/uhoc/type/enums/ExceptionCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cn.uhoc.type.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 错误状态和信息
*/
@Getter
@AllArgsConstructor
public enum ExceptionCode {

// TODO 为每个响应码添加注释
// TODO 学习枚举enum

/**
* 成功
*/
SUCCESS(0, "ok"),
ERR_INPUT_INVALID(8020, "input invalid"),
// ERR_SHOULD_BIND(8021, "should bind failed"),
// ERR_JSON_MARSHAL(8022, "json marshal failed"),
ERR_GET_TASK_INFO(8035, "get task info failed"),
// ERR_GET_TASK_HANDLE_PROCESS(8036, "get task handle process Failed"),
ERR_CREATE_TASK(8037, "create task failed"),
ERR_GET_TASK_LIST(8038, "get task list failed"),
ERR_GET_TASK_CFG(8039, "get task config failed"),
// ERR_INCREASE_CRT_RETRY_NUM(8040, "set task failed"),
// ERR_SET_TASK(8041, "increase crt retry num failed"),
/**
* 获取任务位置失败
*/
ERR_GET_TASK_POS(8042, "get task position failed"),
// ERR_GET_PROCESSING_COUNT(8043, "get processing count failed"),
// ERR_SET_USER_PRIORITY(8045, "set user priority failed"),
// ERR_GET_TASK_CFG_FROM_DB(8046, "get task cfg failed"),
// ERR_SET_TASK_CFG_FROM_DB(8047, "set task cfg failed")
;


private final int code;
private final String info;
}
46 changes: 46 additions & 0 deletions ChronAnt-type/src/main/java/cn/uhoc/type/exception/E.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.uhoc.type.exception;

import cn.uhoc.type.enums.ExceptionCode;
import lombok.Data;

/**
* @program: ChronAnt
* @description:
* @author: chouchouGG
* @create: 2024-12-01 15:48
**/
@Data
public class E extends RuntimeException{

/**
* 异常码
*/
private String code;

/** 异常信息 */
private String info;

public E(int code) {
this.code = String.valueOf(code);
}

public E(int code, Throwable cause) {
this.code = String.valueOf(code);
super.initCause(cause);
}

public E(ExceptionCode r) {
this(r.getCode(), r.getInfo());
}

public E(int code, String info) {
this.code = String.valueOf(code);
this.info = info;
}

public E(int code, String info, Throwable cause) {
this.code = String.valueOf(code);
this.info = info;
super.initCause(cause);
}
}
24 changes: 24 additions & 0 deletions ChronAnt-type/src/main/java/cn/uhoc/type/model/R.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.uhoc.type.model;

import cn.uhoc.type.enums.ExceptionCode;
import lombok.*;

@Data
@Builder
@AllArgsConstructor
public class R<T> {

private int code;
private String info;
private T data;

public R(ExceptionCode responseCode) {
this.code = responseCode.getCode();
this.info = responseCode.getInfo();
}

public R(T data) {
this(ExceptionCode.SUCCESS);
this.data = data;
}
}
11 changes: 0 additions & 11 deletions ChronAnt-type/src/main/java/exception/E.java

This file was deleted.

0 comments on commit fc361e6

Please sign in to comment.