Skip to content

Commit

Permalink
fix the cause and error message of the exception so that it is propag…
Browse files Browse the repository at this point in the history
…ated to parent
  • Loading branch information
itsankit-google committed Sep 2, 2024
1 parent d9e7dd0 commit c54201e
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@
* <li><b>errorReason:</b> A reason for why the error occurred.</li>
* <li><b>errorType:</b> The type of error, represented by the {@ErrorType} enum,
* such as SYSTEM, USER, or UNKNOWN.</li>
* <li><b>cause:</b> The cause of this throwable or null if the cause is nonexistent
* or unknown.</li>
* </ul>
**/
public class ProgramFailureException extends RuntimeException {
private final String errorCategory;
private final String errorMessage;
private final String errorReason;
private final ErrorType errorType;

// Private constructor to prevent direct instantiation
private ProgramFailureException(String errorCategory, String errorMessage, String errorReason,
ErrorType errorType) {
ErrorType errorType, Throwable cause) {
super(errorMessage, cause);
this.errorCategory = errorCategory;
this.errorMessage = errorMessage;
this.errorReason = errorReason;
this.errorType = errorType;
}
Expand All @@ -62,15 +63,6 @@ public String getErrorCategory() {
return errorCategory == null ? "Others" : errorCategory;
}

/**
* Returns the detailed message associated with the error.
*
* @return a {@String} representing the error message.
*/
public String getErrorMessage() {
return errorMessage;
}

/**
* Returns the reason for the error.
*
Expand Down Expand Up @@ -103,6 +95,7 @@ public static class Builder {
private String errorMessage;
private String errorReason;
private ErrorType errorType;
private Throwable cause;

/**
* Sets the error category for the ProgramFailureException.
Expand Down Expand Up @@ -148,13 +141,25 @@ public Builder withErrorType(ErrorType errorType) {
return this;
}

/**
* Sets the cause for the ProgramFailureException.
*
* @param cause the cause (which is saved for later retrieval by the getCause() method).
* @return The current Builder instance.
*/
public Builder withCause(Throwable cause) {
this.cause = cause;
return this;
}

/**
* Builds and returns a new instance of ProgramFailureException.
*
* @return A new ProgramFailureException instance.
*/
public ProgramFailureException build() {
return new ProgramFailureException(errorCategory, errorMessage, errorReason, errorType);
return new ProgramFailureException(errorCategory, errorMessage,
errorReason, errorType, cause);
}
}
}

0 comments on commit c54201e

Please sign in to comment.