gpt4 book ai didi

java - 异常处理状态码

转载 作者:行者123 更新时间:2023-11-30 06:40:22 25 4
gpt4 key购买 nike

在我的项目中,使用类似下面的方法创建了异常处理。我的意思是我们确实在某些服务层中throw new GeekAlreadyExistsException(),最后会返回一些错误响应(取决于错误代码)。

public interface ErrorCode {

String code();

HttpStatus httpStatus();

}

enum GeeksApiErrorCodes implements ErrorCode {
GEEK_ALREADY_EXISTS("geeks-1", HttpStatus.BAD_REQUEST);

private final String code;
private final HttpStatus httpStatus;

GeeksApiErrorCodes(String code, HttpStatus httpStatus) {
this.code = code;
this.httpStatus = httpStatus;
}

@Override
public String code() {
return code;
}

@Override
public HttpStatus httpStatus() {
return httpStatus;
}
}

最佳答案

我建议让异常知道它自己的错误代码,例如像这样:

public abstract class ApplicationException extends RuntimeException {

protected ApplicationException() {
super();
}
protected ApplicationException(String message) {
super(message);
}
protected ApplicationException(Throwable cause) {
super(cause);
}
protected ApplicationException(String message, Throwable cause) {
super(message, cause);
}

public abstract String getErrorCode();

public abstract HttpStatus getHttpStatus();

}
public class GeekAlreadyExistsException extends ApplicationException {
private static final long serialVersionUID = 1L;

public GeekAlreadyExistsException() {
super();
}

public GeekAlreadyExistsException(String message) {
super(message);
}

public GeekAlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}

@Override
public String getErrorCode() {
return "geeks-1";
}

@Override
public HttpStatus getHttpStatus() {
return HttpStatus.BAD_REQUEST;
}

}

如果您不想要每个异常一个错误代码的约束,您可以改为在构造函数调用上传递错误代码。

这仍然允许一些专门的子类异常对错误代码进行硬编码,因此调用者无法指定它。

public class ApplicationException extends RuntimeException {
private static final long serialVersionUID = 1L;

private final String errorCode;

private final HttpStatus httpStatus;

public ApplicationException(String errorCode, HttpStatus httpStatus) {
super();
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
public ApplicationException(String errorCode, HttpStatus httpStatus, String message) {
super(message);
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
public ApplicationException(String errorCode, HttpStatus httpStatus, Throwable cause) {
super(cause);
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
public ApplicationException(String errorCode, HttpStatus httpStatus, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}

public final String getErrorCode() {
return this.errorCode;
}

public final HttpStatus getHttpStatus() {
return this.httpStatus;
}

}
public class GeekAlreadyExistsException extends ApplicationException {
private static final long serialVersionUID = 1L;

public GeekAlreadyExistsException() {
super("geeks-1", HttpStatus.BAD_REQUEST);
}

public GeekAlreadyExistsException(String message) {
super("geeks-1", HttpStatus.BAD_REQUEST, message);
}

public GeekAlreadyExistsException(String message, Throwable cause) {
super("geeks-1", HttpStatus.BAD_REQUEST, message, cause);
}

}

关于java - 异常处理状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58361958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com