在我的项目中,使用下面的方法创建了异常处理。我的意思是,我们在某些服务层中抛出新的(),最后将返回一些错误响应(取决于错误代码)。
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;
}
}发布于 2019-10-13 09:09:29
我建议让异常知道自己的错误代码,例如:
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);
}
}发布于 2019-10-13 09:36:30
用新的异常类映射每个错误代码不是一个好的解决方案。如果将来需要更多的错误代码,则必须创建更多的类。因此,更好的解决方案是只在创建/抛出异常时填充错误代码。详情如下:
package test.file;
public class MyException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
//The error code
private final String errorCode;
//The error message corresponding to the error code. Resolved on the basis of the Locale
private final HttpStatus httpStatus;
public MyException(final String errorCode, final HttpStatus httpStatus) {
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
public MyException(final String errorCode, final HttpStatus httpStatus, final Throwable cause) {
super(cause);
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
public MyException(final String message, final String errorCode, final HttpStatus httpStatus, final Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
this.httpStatus = httpStatus;
}
/**
* Method will return error code.
*
* @return
*/
public String getErrorCode() {
return errorCode;
}
/**
* @return the httpStatus
*/
public HttpStatus getHttpStatus() {
return httpStatus;
}
}在这里,您可以直接检索errorCode并执行适当的操作。这将满足你的要求,你刚才提到的问题。这将是您的问题的最简单的解决方案。
https://stackoverflow.com/questions/58361958
复制相似问题