首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异常处理状态码

异常处理状态码
EN

Stack Overflow用户
提问于 2019-10-13 08:49:58
回答 2查看 194关注 0票数 2

在我的项目中,使用下面的方法创建了异常处理。我的意思是,我们在某些服务层中抛出新的(),最后将返回一些错误响应(取决于错误代码)。

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-10-13 09:09:29

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

代码语言:javascript
复制
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();

}
代码语言:javascript
复制
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;
    }

}

如果您不需要一个错误代码-每个异常约束,则可以将错误代码传递给构造函数调用。

这仍然允许一些特殊的子类异常硬编码错误代码,因此调用方无法指定它。

代码语言:javascript
复制
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;
    }

}
代码语言:javascript
复制
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);
    }

}
票数 2
EN

Stack Overflow用户

发布于 2019-10-13 09:36:30

用新的异常类映射每个错误代码不是一个好的解决方案。如果将来需要更多的错误代码,则必须创建更多的类。因此,更好的解决方案是只在创建/抛出异常时填充错误代码。详情如下:

代码语言:javascript
复制
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并执行适当的操作。这将满足你的要求,你刚才提到的问题。这将是您的问题的最简单的解决方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58361958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档