Skip to content

Commit

Permalink
Add errorCodeType, errorCode & supportedDocURL in mdc tags
Browse files Browse the repository at this point in the history
  • Loading branch information
itsankit-google committed Dec 13, 2024
1 parent 2f8b0e0 commit 2aa0928
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

/**
* Enum representing different types of error codes.
* This enum is used to classify error codes based on their origin or type.
* Currently, it supports HTTP and SQLSTATE error codes.
*/
public enum ErrorCodeType {
HTTP,
SQLSTATE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,41 @@ public static ProgramFailureException getProgramFailureException(ErrorCategory e
.withDependency(dependency)
.build();
}

/**
* Get a ProgramFailureException with the given error information.
*
* @param errorCategory The category of the error.
* @param errorReason The reason for the error.
* @param errorMessage The error message.
* @param errorType The type of error.
* @param dependency The bool representing whether the error is coming from a dependent service.
* @param errorCodeType The string representing
* @param errorCode The int representing error code.
* @param supportedDocumentationUrl The URL to the supported documentation.
* @param cause The cause of the error.
* @return A ProgramFailureException with the given error information.
*/
public static ProgramFailureException getProgramFailureException(ErrorCategory errorCategory,
String errorReason, String errorMessage, ErrorType errorType, boolean dependency,
@Nullable String errorCodeType, @Nullable int errorCode,
@Nullable String supportedDocumentationUrl, @Nullable Throwable cause) {

ProgramFailureException.Builder builder = new ProgramFailureException.Builder();

if (cause != null) {
builder = builder.withCause(cause);
}

return builder
.withErrorCategory(errorCategory)
.withErrorReason(errorReason)
.withErrorMessage(errorMessage)
.withErrorType(errorType)
.withDependency(dependency)
.withErrorCodeType(errorCodeType)
.withErrorCode(errorCode)
.withSupportedDocumentationUrl(supportedDocumentationUrl)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ public class ProgramFailureException extends RuntimeException {
private final String errorReason;
private final ErrorType errorType;
private final boolean dependency;
private final String errorCodeType;
private final int errorCode;
private final String supportedDocumentationUrl;

// Private constructor to prevent direct instantiation
private ProgramFailureException(ErrorCategory errorCategory, String errorMessage,
String errorReason, ErrorType errorType, Throwable cause, boolean dependency) {
String errorReason, ErrorType errorType, Throwable cause, boolean dependency,
String errorCodeType, int errorCode, String supportedDocumentationUrl) {
super(errorMessage, cause);
this.errorCategory = errorCategory;
this.errorReason = errorReason;
this.errorType = errorType;
this.dependency = dependency;
this.errorCodeType = errorCodeType;
this.errorCode = errorCode;
this.supportedDocumentationUrl = supportedDocumentationUrl;
}

/**
Expand Down Expand Up @@ -100,6 +106,33 @@ public boolean isDependency() {
return dependency;
}

/**
* Returns the type of error code.
*
* @return the type of error code.
*/
public String getErrorCodeType() {
return errorCodeType;
}

/**
* Returns the error code.
*
* @return the error code.
*/
public int getErrorCode() {
return errorCode;
}

/**
* Returns the URL to the documentation.
*
* @return the URL to the documentation.
*/
public String getSupportedDocumentationUrl() {
return supportedDocumentationUrl;
}

/**
* Builder class for ProgramFailureException.
*/
Expand All @@ -109,7 +142,10 @@ public static class Builder {
private String errorReason;
private ErrorType errorType;
private Throwable cause;
private String errorCodeType;
private int errorCode;
private boolean dependency;
private String supportedDocumentationUrl;

/**
* Sets the error category for the ProgramFailureException.
Expand Down Expand Up @@ -177,14 +213,48 @@ public Builder withDependency(boolean dependency) {
return this;
}

/**
* Sets the error code type for the ProgramFailureException.
*
* @param errorCodeType The type of error code.
* @return The current Builder instance.
*/
public Builder withErrorCodeType(String errorCodeType) {
this.errorCodeType = errorCodeType;
return this;
}

/**
* Sets the error code for the ProgramFailureException.
*
* @param errorCode The error code.
* @return The current Builder instance.
*/
public Builder withErrorCode(int errorCode) {
this.errorCode = errorCode;
return this;
}

/**
* Sets the supported documentation URL for the ProgramFailureException.
*
* @param supportedDocumentationUrl The URL to the documentation.
* @return The current Builder instance.
*/
public Builder withSupportedDocumentationUrl(String supportedDocumentationUrl) {
this.supportedDocumentationUrl = supportedDocumentationUrl;
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, cause, dependency);
errorReason, errorType, cause, dependency,
errorCodeType, errorCode, supportedDocumentationUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,9 @@ public static final class Logging {
public static final String TAG_ERROR_CATEGORY = "errorCategory";
public static final String TAG_ERROR_REASON = "errorReason";
public static final String TAG_ERROR_TYPE = "errorType";
public static final String TAG_ERROR_CODE_TYPE = "errorCodeType";
public static final String TAG_ERROR_CODE = "errorCode";
public static final String TAG_SUPPORTED_DOC_URL = "supportDocUrl";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.elasticsearch.common.Strings;

/**
* CDAP log appender interface.
Expand Down Expand Up @@ -139,6 +140,17 @@ private void addErrorClassificationTags(ILoggingEvent event, Map<String, String>
((ProgramFailureException) throwable).getErrorReason());
modifiableMDC.put(Constants.Logging.TAG_ERROR_TYPE,
((ProgramFailureException) throwable).getErrorType().name());
String errorCodeType = ((ProgramFailureException) throwable).getErrorCodeType();
int errorCode = ((ProgramFailureException) throwable).getErrorCode();
String supportedDocURL =
((ProgramFailureException) throwable).getSupportedDocumentationUrl();
if (!Strings.isNullOrEmpty(errorCodeType)) {
modifiableMDC.put(Constants.Logging.TAG_ERROR_CODE_TYPE, errorCodeType);
modifiableMDC.put(Constants.Logging.TAG_ERROR_CODE, String.valueOf(errorCode));
}
if(!Strings.isNullOrEmpty(supportedDocURL)) {
modifiableMDC.put(Constants.Logging.TAG_SUPPORTED_DOC_URL, supportedDocURL);
}
}
throwable = throwable.getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.inject.Injector;
import io.cdap.cdap.api.dataset.lib.CloseableIterator;
import io.cdap.cdap.api.exception.ErrorCategory;
import io.cdap.cdap.api.exception.ErrorCodeType;
import io.cdap.cdap.api.exception.ErrorType;
import io.cdap.cdap.api.exception.ProgramFailureException;
import io.cdap.cdap.api.exception.WrappedStageException;
Expand Down Expand Up @@ -76,6 +77,9 @@ public void testErrorClassificationTagsArePresent() {
.withErrorCategory(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN))
.withErrorReason("error Reason")
.withErrorType(ErrorType.USER)
.withErrorCodeType(ErrorCodeType.HTTP.name())
.withErrorCode(403)
.withSupportedDocumentationUrl("http://www.example.com")
.build(), "stageName"));
appender.stop();

Expand All @@ -93,10 +97,16 @@ public void testErrorClassificationTagsArePresent() {
String errorCategory = mdc.get(Logging.TAG_ERROR_CATEGORY);
String errorReason = mdc.get(Logging.TAG_ERROR_REASON);
String errorType = mdc.get(Logging.TAG_ERROR_TYPE);
String errorCodeType = mdc.get(Logging.TAG_ERROR_CODE_TYPE);
String errorCode = mdc.get(Logging.TAG_ERROR_CODE);
String supportedDocumentationUrl = mdc.get(Logging.TAG_SUPPORTED_DOC_URL);
Assert.assertEquals("stageName", failedStage);
Assert.assertEquals("Plugin", errorCategory);
Assert.assertEquals("error Reason", errorReason);
Assert.assertEquals("USER", errorType);
Assert.assertEquals("HTTP", errorCodeType);
Assert.assertEquals(403, Integer.parseInt(errorCode));
Assert.assertEquals("http://www.example.com", supportedDocumentationUrl);
}

@AfterClass
Expand Down

0 comments on commit 2aa0928

Please sign in to comment.