Created
September 11, 2020 11:17
-
-
Save shiwork/1d4f6e73e26f5fea9c4c185852a001c3 to your computer and use it in GitHub Desktop.
Handling row not found error from spanner and yo package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package errors | |
import ( | |
"regexp" | |
"cloud.google.com/go/spanner" | |
"google.golang.org/grpc/codes" | |
"google.golang.org/grpc/status" | |
) | |
const ( | |
yoNotFoundMessage = "no more items in iterator" | |
spannerNotFoundMessage = "row not found" | |
) | |
type YoError interface { | |
Error() string | |
Unwrap() error | |
DBTableName() string | |
GRPCStatus() *status.Status | |
Timeout() bool | |
Temporary() bool | |
NotFound() bool | |
} | |
func IsSpannerRecordNotFound(err error) bool { | |
yoErr, ok := err.(YoError) | |
if !ok { | |
// check spanner | |
if codes.NotFound != spanner.ErrCode(err) { | |
return false | |
} | |
matched, _ := regexp.MatchString(spannerNotFoundMessage, err.Error()) | |
return matched | |
} | |
if !yoErr.NotFound() { | |
return false | |
} | |
matched, _ := regexp.MatchString(yoNotFoundMessage, yoErr.Error()) | |
return matched | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment