Skip to content

Commit

Permalink
Merge pull request #15698 from cdapio/CDAP-21059-error-management-and…
Browse files Browse the repository at this point in the history
…-classification

[CDAP-21059] introduce error category class
  • Loading branch information
itsankit-google authored Sep 13, 2024
2 parents 8d3e81f + 3c6b832 commit 42c2401
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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;

/**
* Class representing the category of an error.
*
* <p>This class is used to classify errors into different categories,
* such as plugin errors, provisioning errors, etc.
* </p>
*/
public class ErrorCategory {
private final ErrorCategoryEnum errorCategory;
private final String subCategory;

/**
* Constructor for ErrorCategory.
*
* @param errorCategory The category of the error.
*/
public ErrorCategory(ErrorCategoryEnum errorCategory) {
this.errorCategory = errorCategory;
this.subCategory = null;
}

/**
* Constructor for ErrorCategory.
*
* @param errorCategory The category of the error.
* @param subCategory The sub-category of the error.
*/
public ErrorCategory(ErrorCategoryEnum errorCategory, String subCategory) {
this.errorCategory = errorCategory;
this.subCategory = subCategory;
}

/*
* Returns the category of the error.
*/
public String getErrorCategory() {
return errorCategory == null ? ErrorCategoryEnum.OTHERS.toString() : subCategory == null ?

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

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck

'?' should be on a new line.
errorCategory.toString() : String.format("%s-'%s'", errorCategory, subCategory);
}

/*
* Returns a string representation of the error category.
*/
@Override
public String toString() {
return getErrorCategory();
}

/**
* Enum representing the different categories of errors.
*/
public enum ErrorCategoryEnum {
PLUGIN("Plugin"),
NETWORKING("Networking"),
PROVISIONING("Provisioning"),
ACCESS("Access"),
SCHEDULES_AND_TRIGGERS("Schedules and Triggers"),
OTHERS("Others");

private final String displayName;

ErrorCategoryEnum(String name) {
displayName = name;
}

/**
* Returns a string representation of the error category enum.
*/
@Override
public String toString() {
return displayName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
* </ul>
**/
public class ProgramFailureException extends RuntimeException {
private final String errorCategory;
private final ErrorCategory errorCategory;
private final String errorReason;
private final ErrorType errorType;

// Private constructor to prevent direct instantiation
private ProgramFailureException(String errorCategory, String errorMessage, String errorReason,
private ProgramFailureException(ErrorCategory errorCategory, String errorMessage, String errorReason,
ErrorType errorType, Throwable cause) {
super(errorMessage, cause);
this.errorCategory = errorCategory;
Expand All @@ -60,7 +60,7 @@ private ProgramFailureException(String errorCategory, String errorMessage, Strin
* @return a {@String} representing the error category.
*/
public String getErrorCategory() {
return errorCategory == null ? "Others" : errorCategory;
return errorCategory.getErrorCategory();
}

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public ErrorType getErrorType() {
* Builder class for ProgramFailureException.
*/
public static class Builder {
private String errorCategory;
private ErrorCategory errorCategory;
private String errorMessage;
private String errorReason;
private ErrorType errorType;
Expand All @@ -103,7 +103,7 @@ public static class Builder {
* @param errorCategory The category of the error.
* @return The current Builder instance.
*/
public Builder withErrorCategory(String errorCategory) {
public Builder withErrorCategory(ErrorCategory errorCategory) {
this.errorCategory = errorCategory;
return this;
}
Expand Down

0 comments on commit 42c2401

Please sign in to comment.