View Javadoc
1   package top.infra.core;
2   
3   import lombok.Getter;
4   
5   /**
6    * ExpectedExceptionWithCode is thrown by user, not by libraries or frameworks,
7    * with an error code to identify error more clearly.
8    */
9   public class ExpectedExceptionWithCode extends ExpectedException {
10  
11    private final ErrorCode code;
12  
13    /** Constructs a new expected exception with {@code null} as its
14     * detail message.  The cause is not initialized, and may subsequently be
15     * initialized by a call to {@link #initCause}.
16     *
17     * @param code the detail error code and message (which is saved for later retrieval
18     *        by the {@link #getCode()} and {@link #getMessage()} method).
19     */
20    public ExpectedExceptionWithCode(final ErrorCode code) {
21      super(code != null ? code.getMessage().getText() : "");
22      this.code = code;
23    }
24  
25    /**
26     * Constructs a new expected exception with the specified detail message and
27     * cause.  <p>Note that the detail message associated with
28     * {@code cause} is <i>not</i> automatically incorporated in
29     * this expected exception's detail message.
30     *
31     * @param  code the detail error code and message (which is saved for later retrieval
32     *         by the {@link #getCode()} and {@link #getMessage()} method).
33     * @param  cause the cause (which is saved for later retrieval by the
34     *         {@link #getCause()} method).  (A <tt>null</tt> value is
35     *         permitted, and indicates that the cause is nonexistent or
36     *         unknown.)
37     */
38    public ExpectedExceptionWithCode(final ErrorCode code, final Throwable cause) {
39      super(code != null ? code.getMessage().getText() : "", cause);
40      this.code = code;
41    }
42  
43    /**
44     * Constructs a new expected exception with the specified detail
45     * message, cause, suppression enabled or disabled, and writable
46     * stack trace enabled or disabled.
47     *
48     * @param  code the detail error code and message (which is saved for later retrieval
49     *         by the {@link #getCode()} and {@link #getMessage()} method).
50     * @param cause the cause.  (A {@code null} value is permitted,
51     * and indicates that the cause is nonexistent or unknown.)
52     * @param enableSuppression whether or not suppression is enabled
53     *                          or disabled
54     * @param writableStackTrace whether or not the stack trace should
55     *                           be writable
56     */
57    protected ExpectedExceptionWithCode( //
58        final ErrorCode code, //
59        final Throwable cause, //
60        boolean enableSuppression, //
61        boolean writableStackTrace //
62    ) {
63      super(code != null ? code.getMessage().getText() : "", cause, enableSuppression, writableStackTrace);
64      this.code = code;
65    }
66  
67    public ErrorCode getCode() {
68      return this.code;
69    }
70  }