Skip to content

Add util function getProgramFailureExceptionFromSQLException#1954

Merged
psainics merged 1 commit intocdapio:developfrom
cloudsufi:dberror-util
Mar 27, 2025
Merged

Add util function getProgramFailureExceptionFromSQLException#1954
psainics merged 1 commit intocdapio:developfrom
cloudsufi:dberror-util

Conversation

@psainics
Copy link
Copy Markdown

Adding a util function that can be used to getProgramFailureExceptionFromSQLException.

Ref: data-integrations/database-plugins#568 (comment)

This code snippet can be modified to

    try {
      executeQuery.run();
    } catch (Exception e) {
      if (e instanceof SQLException) {
        throw DBErrorDetailsProvider.getProgramFailureExceptionFromSQLException((SQLException) e);
      }
      FailureCollector collector = context.getFailureCollector();
      collector.addFailure("Failed to execute query with message: " + e.getMessage(), null)
          .withStacktrace(e.getStackTrace());
      collector.getOrThrowException();
    }

Test

image

Moving default implementation to static methods, making the function static may lead to overriding issue in other projects.

@psainics psainics added the build Trigger unit test build label Mar 26, 2025
@psainics psainics self-assigned this Mar 26, 2025
Comment on lines +183 to +200
public static ProgramFailureException getProgramFailureExceptionFromSQLException(SQLException e) {
String errorMessage = String.format("SQL Exception occurred: [Message='%s', SQLState='%s', ErrorCode='%s'].",
e.getMessage(), e.getSQLState(), e.getErrorCode());
String sqlState = e.getSQLState();
int errorCode = e.getErrorCode();
String errorMessageWithDetails = String.format(
"SQL Error occurred, sqlState: '%s', errorCode: '%s', errorMessage: %s", sqlState, errorCode, errorMessage);
if (!errorMessage.endsWith(".")) {
errorMessage = errorMessage + ".";
}
errorMessage = String.format("%s For more details, see %s", errorMessage, EXTERNAL_DOCUMENTATION_LINK);
return ErrorUtils.getProgramFailureException(
Strings.isNullOrEmpty(sqlState) ? new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN)
: getErrorCategoryFromSqlStateDefault(sqlState), errorMessage, errorMessageWithDetails,
getErrorTypeFromErrorCodeAndSqlStateDefault(errorCode, sqlState), true, ErrorCodeType.SQLSTATE, sqlState,
EXTERNAL_DOCUMENTATION_LINK, e);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can modify the below function itself to take external_documentation_link as param.

Also errorContext can be nullable if null, we can fallback to the errorMessage proposed in this function.

Please do not introduce duplicate code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing all the static methods, it was not helpfull.

This will be the new code

    try {
      executeQuery.run();
    } catch (Exception e) {
      if (e instanceof SQLException) {
        DBErrorDetailsProvider dbe = new DBErrorDetailsProvider();
        throw dbe.getProgramFailureException((SQLException) e, null);
      }
      FailureCollector collector = context.getFailureCollector();
      collector.addFailure("Failed to execute query with message: " + e.getMessage(), null)
          .withStacktrace(e.getStackTrace());
      collector.getOrThrowException();
    }

Rename DEFAULT_EXTERNAL_DOCUMENTATION_LINK
@psainics psainics merged commit c15b9e3 into cdapio:develop Mar 27, 2025
5 checks passed
@psainics psainics deleted the dberror-util branch March 27, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Trigger unit test build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants