Skip to content

Commit 5a6a79e

Browse files
committed
doc: add Connection.Do result processing examples
- ExampleConnection_Do demonstrates how to process a result. - ExampleConnection_Do_failure demonstrates how to process a request failure. Closes #128
1 parent 483aa4b commit 5a6a79e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

example_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,75 @@ func ExampleSpace() {
998998
// SpaceField 2 name3 unsigned
999999
}
10001000

1001+
// ExampleConnection_Do demonstrates how to send a request and process
1002+
// a response.
1003+
func ExampleConnection_Do() {
1004+
conn := exampleConnect(opts)
1005+
defer conn.Close()
1006+
1007+
// It could be any request.
1008+
req := tarantool.NewReplaceRequest("test").
1009+
Tuple([]interface{}{int(1111), "foo", "bar"})
1010+
1011+
// We got a future, the request actually not performed yet.
1012+
future := conn.Do(req)
1013+
1014+
// When the future receives the response, the result of the Future is set
1015+
// and becomes available. We could wait for that moment with Future.Get()
1016+
// or Future.GetTyped() methods.
1017+
resp, err := future.Get()
1018+
if err != nil {
1019+
fmt.Printf("Failed to execute the request: %s\n", err)
1020+
} else {
1021+
fmt.Println(resp.Data)
1022+
}
1023+
1024+
// Output:
1025+
// [[1111 foo bar]]
1026+
}
1027+
1028+
// ExampleConnection_Do_failure demonstrates how to send a request and process
1029+
// failure.
1030+
func ExampleConnection_Do_failure() {
1031+
conn := exampleConnect(opts)
1032+
defer conn.Close()
1033+
1034+
// It could be any request.
1035+
req := tarantool.NewCallRequest("not_exist")
1036+
1037+
// We got a future, the request actually not performed yet.
1038+
future := conn.Do(req)
1039+
1040+
// When the future receives the response, the result of the Future is set
1041+
// and becomes available. We could wait for that moment with Future.Get()
1042+
// or Future.GetTyped() methods.
1043+
resp, err := future.Get()
1044+
if err != nil {
1045+
// We don't print the error here to keep the example reproducible.
1046+
// fmt.Printf("Failed to execute the request: %s\n", err)
1047+
if resp == nil {
1048+
// Something happens in a client process (timeout, IO error etc).
1049+
fmt.Printf("Resp == nil, ClientErr = %s\n", err.(tarantool.ClientError))
1050+
} else {
1051+
// Response exist. So it could be a Tarantool error or a decode
1052+
// error. We need to check the error code.
1053+
fmt.Printf("Error code from the response: %d\n", resp.Code)
1054+
if resp.Code == tarantool.OkCode {
1055+
fmt.Printf("Decode error: %s\n", err)
1056+
} else {
1057+
code := err.(tarantool.Error).Code
1058+
fmt.Printf("Error code from the error: %d\n", code)
1059+
fmt.Printf("Error short from the error: %s\n", code)
1060+
}
1061+
}
1062+
}
1063+
1064+
// Output:
1065+
// Error code from the response: 33
1066+
// Error code from the error: 33
1067+
// Error short from the error: ER_NO_SUCH_PROC
1068+
}
1069+
10011070
// To use prepared statements to query a tarantool instance, call NewPrepared.
10021071
func ExampleConnection_NewPrepared() {
10031072
// Tarantool supports SQL since version 2.0.0

0 commit comments

Comments
 (0)