Repository: snowflakedb/gosnowflake
Pull request #539 (2022-01-28 01:58:20)

SNOW-535399 Fix GetQueryStatus Bug
### Description
Change ErrorCode to type string

### Checklist
- [ ] Code compiles correctly
- [ ] Run ``make fmt`` to fix inconsistent formats
- [ ] Run ``make lint`` to get lint errors and fix all of them
- [ ] Created tests which fail without the change (if possible)
- [ ] All tests passing
- [ ] Extended the README / documentation, if necessary



Patch:
diff --git a/errors.go b/errors.go
index f59641d..06b5f2a 100644
--- a/errors.go
+++ b/errors.go
@@ -266,6 +266,7 @@ const (
 	errMsgInternalNotMatchEncryptMaterial    = "number of downloading files doesn't match the encryption materials. files=%v, encmat=%v"
 	errMsgFailedToConvertToS3Client          = "failed to convert interface to s3 client"
 	errMsgNoResultIDs                        = "no result IDs returned with the multi-statement query"
+	errMsgQueryStatus                        = "server ErrorCode=%s, ErrorMessage=%s"
 )
 
 var (
diff --git a/monitoring.go b/monitoring.go
index f821596..fdf07d3 100644
--- a/monitoring.go
+++ b/monitoring.go
@@ -79,7 +79,7 @@ type retStatus struct {
 	SQLText      string   `json:"sqlText"`
 	StartTime    int64    `json:"startTime"`
 	EndTime      int64    `json:"endTime"`
-	ErrorCode    int      `json:"errorCode"`
+	ErrorCode    string   `json:"errorCode"`
 	ErrorMessage string   `json:"errorMessage"`
 	Stats        retStats `json:"stats"`
 }
@@ -107,7 +107,7 @@ type SnowflakeQueryStatus struct {
 	SQLText      string
 	StartTime    int64
 	EndTime      int64
-	ErrorCode    int
+	ErrorCode    string
 	ErrorMessage string
 	ScanBytes    int64
 	ProducedRows int64
@@ -160,11 +160,11 @@ func (sc *snowflakeConn) checkQueryStatus(
 	}
 
 	queryRet := statusResp.Data.Queries[0]
-	if queryRet.ErrorCode != 0 {
+	if queryRet.ErrorCode != "" {
 		return &queryRet, (&SnowflakeError{
-			Number: ErrQueryStatus,
-			Message: fmt.Sprintf("server ErrorCode=%d, ErrorMessage=%s",
-				queryRet.ErrorCode, queryRet.ErrorMessage),
+			Number:         ErrQueryStatus,
+			Message:        errMsgQueryStatus,
+			MessageArgs:    []interface{}{queryRet.ErrorCode, queryRet.ErrorMessage},
 			IncludeQueryID: true,
 			QueryID:        qid,
 		}).exceptionTelemetry(sc)


Test patch:
diff --git a/connection_test.go b/connection_test.go
index b895917..cf4b00b 100644
--- a/connection_test.go
+++ b/connection_test.go
@@ -275,7 +275,7 @@ func customGetQuery(ctx context.Context, rest *snowflakeRestful, url *url.URL,
 func returnQueryIsRunningStatus(ctx context.Context, rest *snowflakeRestful, fullURL *url.URL,
 	vals map[string]string, duration time.Duration) (*http.Response, error) {
 	jsonStr := `{"data" : { "queries" : [{"status" : "RUNNING", "state" :
-		"FILE_SET_INITIALIZATION", "errorCode" : 0, "errorMessage" : null}] },
+		"FILE_SET_INITIALIZATION", "errorCode" : "", "errorMessage" : null}] },
 		"code" : null, "message" : null, "success" : true }`
 	return customGetQuery(ctx, rest, fullURL, vals, duration, jsonStr)
 }
@@ -283,7 +283,7 @@ func returnQueryIsRunningStatus(ctx context.Context, rest *snowflakeRestful, ful
 func returnQueryIsErrStatus(ctx context.Context, rest *snowflakeRestful, fullURL *url.URL,
 	vals map[string]string, duration time.Duration) (*http.Response, error) {
 	jsonStr := `{"data" : { "queries" : [{"status" : "FAILED_WITH_ERROR",
-		"errorCode" : 0, "errorMessage" : ""}] }, "code" : null, "message" :
+		"errorCode" : "", "errorMessage" : ""}] }, "code" : null, "message" :
 		null, "success" : true }`
 	return customGetQuery(ctx, rest, fullURL, vals, duration, jsonStr)
 }
@@ -406,9 +406,34 @@ func TestGetQueryStatus(t *testing.T) {
 	if err != nil {
 		t.Error(err)
 	}
+	if qStatus == nil {
+		t.Fatal("there was no query status returned")
+	}
 
-	if qStatus.ErrorCode != 0 || qStatus.ScanBytes != 1536 || qStatus.ProducedRows != 10 {
+	if qStatus.ErrorCode != "" || qStatus.ScanBytes != 1536 || qStatus.ProducedRows != 10 {
 		t.Errorf("expected no error. got: %v, scan bytes: %v, produced rows: %v",
 			qStatus.ErrorCode, qStatus.ScanBytes, qStatus.ProducedRows)
 	}
 }
+
+func TestGetInvalidQueryStatus(t *testing.T) {
+	config, err := ParseDSN(dsn)
+	if err != nil {
+		t.Error(err)
+	}
+	ctx := context.Background()
+	sc, err := buildSnowflakeConn(ctx, *config)
+	if err != nil {
+		t.Error(err)
+	}
+	if err = authenticateWithConfig(sc); err != nil {
+		t.Error(err)
+	}
+
+	sc.rest.RequestTimeout = 1 * time.Second
+
+	qStatus, err := sc.checkQueryStatus(ctx, "1234")
+	if err == nil || qStatus != nil {
+		t.Error("expected an error")
+	}
+}