GO SDK is not throwing an error when we use named parameters

Hi all,

Recently, I faced a weird issue on GO SDK. I am using named parameters to form the n1ql query and while passing the values for those params I mistakenly given the wrong input type (string instead of int) for limit and offset. Since the input params of type interface it doesn’t thrown an error while compiling the code. While execution the query was successfully executed and returned the empty response. Below is the snippet of the code for both failure and success.

// Database connect
cluster, err := gocb.Connect(cbConnStr, gocb.ClusterOptions{
	Username: "*******",
	Password: "********",
	TimeoutsConfig: gocb.TimeoutsConfig{
		QueryTimeout: 10*time.Second,
	},
})

if err != nil {
	logger.BootstrapLogger.Error(err)
	panic(err)
}

Failure case

queryStr := "SELECT * from `bucket-name` WHERE name=$name limit $limit offset $offset"
params["name"] = name
params["limit"] = limitVal //string
params["offset"] = offsetVal //string
rows, err = cluster.Query(queryStr, &gocb.QueryOptions{
	NamedParameters: params,
})
if err != nil {
	return nil, parseDatabaseError(err, logFields)
}

Success Case

queryStr := "SELECT * from `bucket-name` WHERE name=$name limit $limit offset $offset"
params["name"] = name
params["limit"] = limitVal //int
params["offset"] = offsetVal //int
rows, err = cluster.Query(queryStr, &gocb.QueryOptions{
	NamedParameters: params,
})
if err != nil {
	return nil, parseDatabaseError(err, logFields)
}

I have resolved the issue by changing the datatype of the params. But, my question is why the SDK doesn’t throw an error when I pass invalid type of values. Ideally, It should throw an error rather it is executing the query and returning the empty response.

Kindly, Please help on this.

Hi @ajithkumarm the Go SDK is surfacing what the query engine is responding with here. For whatever reason (someone from the query team will be better placed to comment on) the query itself returns a 200 status, however there are errors in the response.

As illustrated in Query | Couchbase Docs you should always check the .Err() value for the result if Query itself doesn’t return an error. e.g.

	for rows.Next() {
		var row interface{}
		err = rows.Row(&row)
		if err != nil {
			panic(err)
		}
	}

	err = rows.Err()
	if err != nil {
		panic(err)
	}

When i do this with a named parameter of limit set to a string of “1” I see an error something like

panic: internal server failure | {"statement":"SELECT * from `default` LIMIT $limit","errors":[{"code":5030,"message":"Invalid LIMIT value 1."}],"http_status_code":200}

thank you so much @chvck for the clarification

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.