异常处理状态码

时间:2019-10-13 08:49:58

标签: java spring spring-boot exception

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

Activity

2 个答案:

答案 0 :(得分:2)

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

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);
    }

}

答案 1 :(得分:2)

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

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并执行适当的操作。这将满足您的要求。这将是解决您问题的最简单方法。