Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spanner): wrap errors in inline begin transaction to avoid masking #8273

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

harshachinta
Copy link
Contributor

@harshachinta harshachinta commented Jul 17, 2023

When inline begin transaction was added, the error messages were getting masked with

spanner: code = "Internal", desc = "failed inline begin transaction"

This PR fixes this by wrapping the error to unmask original errors.

@harshachinta harshachinta requested review from a team as code owners July 17, 2023 13:37
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the Spanner API. labels Jul 17, 2023
@rahul2393
Copy link
Contributor

@harshachinta Thanks for picking this up, please add the test case to mock the invalid SQL statement and check if the invalid SQL error message is contained in the final error.

@product-auto-label product-auto-label bot added the stale: old Pull request is old and needs attention. label Aug 17, 2023
@harshachinta harshachinta added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 31, 2023
@product-auto-label product-auto-label bot added stale: extraold Pull request is critically old and needs prioritization. and removed stale: old Pull request is old and needs attention. labels Sep 16, 2023
@ajorgensen
Copy link

@harshachinta I'd be happy to take over this changeset and help push it through if thats ok with you. We run into this fairly frequently so this change would be greatly beneficial.

@harshachinta
Copy link
Contributor Author

@ajorgensen Feel free to work on top of this. I guess the changes in this PR is working as expected except there was some test failures. I don't exactly recall the pending stuff in this PR. Will try taking a look at this next week.

@ajorgensen
Copy link

ajorgensen commented Dec 4, 2023

@harshachinta I found one more spot that looks to be missing a bit of extra information that is available.

EDIT: Never mind I figured it out... required a bit of url hacking on the pull-request screen to make a PR against your fork but I think this should work: harshachinta#5

Github doesn't seem to want to allow me to make a PR against your fork of the repo for some reason but the change is here: https://github.com/ajorgensen/google-cloud-go/tree/wrap-inline-begin-transaction-error

or if its easier here is the patch:

diff --git a/spanner/transaction.go b/spanner/transaction.go
index dab7ceb76a..6fedf82165 100644
--- a/spanner/transaction.go
+++ b/spanner/transaction.go
@@ -1216,14 +1216,22 @@ func (t *ReadWriteTransaction) batchUpdateWithOptions(ctx context.Context, stmts
 		}
 		counts = append(counts, count)
 	}
+
+	if resp.Status != nil && resp.Status.Code != 0 {
+		err = spannerErrorf(codes.Code(uint32(resp.Status.Code)), resp.Status.Message)
+		if hasInlineBeginTransaction && !haveTransactionID {
+			t.setTransactionID(nil)
+			return counts, toSpannerErrorDuringInlineBegin(err)
+		}
+
+		return counts, err
+	}
+
 	if hasInlineBeginTransaction && !haveTransactionID {
 		// retry with explicit BeginTransaction
 		t.setTransactionID(nil)
 		return counts, errInlineBeginTransactionFailed()
 	}
-	if resp.Status != nil && resp.Status.Code != 0 {
-		return counts, spannerErrorf(codes.Code(uint32(resp.Status.Code)), resp.Status.Message)
-	}
 	return counts, nil
 }

here is a fix for the failing client_test as well:

diff --git a/spanner/session.go b/spanner/session.go
index 547ed464bc..27c309ad68 100644
--- a/spanner/session.go
+++ b/spanner/session.go
@@ -1656,7 +1656,7 @@ func isFailedInlineBeginTransaction(err error) bool {
 	if err == nil {
 		return false
 	}
-	return strings.Contains(err.Error(), errInlineBeginTransactionFailed().Error())
+	return strings.Contains(err.Error(), inlineBeginTransactionFailedMsg)
 }
 
 // isClientClosing returns true if the given error is a

@rahul2393 rahul2393 removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jan 3, 2024
@codyoss
Copy link
Member

codyoss commented Jul 24, 2024

@rahul2393 do we still want to accecpt this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. size: m Pull request size is medium. stale: extraold Pull request is critically old and needs prioritization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants