Skip to content

Commit

Permalink
Introduce ProgramFailureException and WrappedStageException to be con…
Browse files Browse the repository at this point in the history
…sumed by platform and plugins
  • Loading branch information
itsankit-google committed Aug 20, 2024
1 parent cbcb4a9 commit 860d982
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.cdap.api.exception;

Check warning on line 16 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ErrorType.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

'package' should be separated from previous line.

/**
* Enum representing different types of errors due to which a program can fail.
* <p>
* This enum classifies errors into three broad categories:
* </p>
* <ul>
* <li><b>SYSTEM</b> - Represents system-level errors that are typically caused by infrastructure,
* network, or external dependencies, rather than user actions.</li>
* <li><b>USER</b> - Represents errors caused by user input or actions that violate expected rules
* or constraints.</li>
* <li><b>UNKNOWN</b> - Represents errors of an indeterminate type, where the cause cannot be
* classified as either system or user error.</li>
* </ul>
*/
public enum ErrorType {
SYSTEM,
USER,
UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.cdap.api.exception;

Check warning on line 16 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

'package' should be separated from previous line.

/**
* Exception class representing a program failure with detailed error information.
* <p>
* This exception is used to indicate failures within the system and provides details about
* the error category, message, reason, and type. It uses the builder pattern to allow
* for flexible construction of the exception with optional fields.
* </p>
* <p>
* The following details can be provided:
* </p>
* <ul>
* <li><b>errorCategory:</b> A high-level classification of the error.</li>
* <li><b>errorMessage:</b> A detailed description of the error message.</li>
* <li><b>errorReason:</b> A reason for why the error occurred.</li>
* </ul>
**/
public class ProgramFailureException extends RuntimeException {
private String errorCategory;
private String errorMessage;
private String errorReason;

// Private constructor to prevent direct instantiation
private ProgramFailureException(String errorCategory, String errorMessage, String errorReason) {
this.errorCategory = errorCategory;
this.errorMessage = errorMessage;
this.errorReason = errorReason;
}

/**
* Returns the category of the error.
* <p>

Check warning on line 48 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck

<p> tag should be placed immediately before the first word, with no space after.

Check warning on line 48 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck

<p> tag should be preceded with an empty line.
* This typically provides a high-level classification of the error,
* such as plugin, provisioning, etc.
* If the category or reason is not known - it will be marked as ‘Others’.
*
* @return a {@String} representing the error category.
*/
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.
* <p>

Check warning on line 70 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck

<p> tag should be placed immediately before the first word, with no space after.

Check warning on line 70 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck

<p> tag should be preceded with an empty line.
* The reason usually explains why the error occurred, such as a specific validation failure
* or an unexpected condition.
*
* @return a {@String} representing the error reason.
*/
public String getErrorReason() {
return errorReason;
}

/**
* Builder class for ProgramFailureException.
*/
public static class Builder {
private String errorCategory;
private String errorMessage;
private String errorReason;

/**
* Sets the error category for the ProgramFailureException.
* @param errorCategory The category of the error.

Check warning on line 90 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.RequireEmptyLineBeforeBlockTagGroupCheck

Javadoc tag '@param' should be preceded with an empty line.
* @return The current Builder instance.
*/
public Builder withErrorCategory(String errorCategory) {
this.errorCategory = errorCategory;
return this;
}

/**
* Sets the error message for the ProgramFailureException.
* @param errorMessage The detailed error message.

Check warning on line 100 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.RequireEmptyLineBeforeBlockTagGroupCheck

Javadoc tag '@param' should be preceded with an empty line.
* @return The current Builder instance.
*/
public Builder withErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}

/**
* Sets the error reason for the ProgramFailureException.
* @param errorReason The reason for the error.

Check warning on line 110 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.RequireEmptyLineBeforeBlockTagGroupCheck

Javadoc tag '@param' should be preceded with an empty line.
* @return The current Builder instance.
*/
public Builder withErrorReason(String errorReason) {
this.errorReason = errorReason;
return this;
}

/**
* Builds and returns a new instance of ProgramFailureException.
* @return A new ProgramFailureException instance.

Check warning on line 120 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/ProgramFailureException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.RequireEmptyLineBeforeBlockTagGroupCheck

Javadoc tag '@return' should be preceded with an empty line.
*/
public ProgramFailureException build() {
return new ProgramFailureException(errorCategory, errorMessage, errorReason);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.cdap.api.exception;

Check warning on line 16 in cdap-api-common/src/main/java/io/cdap/cdap/api/exception/WrappedStageException.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck

'package' should be separated from previous line.

/**
* Exception class that wraps another exception and includes the name of the stage
* where the failure occurred.
* <p>
* This class extends {@code RuntimeException} and is used to provide additional context when
* an error occurs during a specific stage of execution.
* The original cause of the error is preserved, and the stage name where the failure happened is
* included for better error tracking.
* </p>
*/
public class WrappedStageException extends RuntimeException {

private final String stageName;

/**
* Constructs a new {@code WrappedStageException} with the specified cause and stage name.
*
* @param cause the original exception that caused the failure.
* @param stageName the name of the stage where the failure occurred.
*/
public WrappedStageException(Throwable cause, String stageName) {
super(cause);
this.stageName = stageName;
}

/**
* Returns the name of the stage where the exception occurred.
*
* @return the stage name as a {@String}.
*/
public String getStageName() {
return stageName;
}
}

0 comments on commit 860d982

Please sign in to comment.