Skip to content

Commit aa2dafa

Browse files
committed
errors: added CodeErrors
Revised error use, added new type CodeErrors to compare them using errors.Is(). Also used to check that request's ctx was cancelled. Fixes (#457)
1 parent 0546ab3 commit aa2dafa

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func (conn *Connection) newFuture(req Request) (fut *Future) {
984984
if ctx != nil {
985985
select {
986986
case <-ctx.Done():
987-
fut.SetError(fmt.Errorf("context is done (request ID %d)", fut.requestId))
987+
fut.SetError(fmt.Errorf("context is done (request ID %d) (%w)", fut.requestId, ErrCancelledCtx))
988988
shard.rmut.Unlock()
989989
return
990990
default:

errors.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func (tnterr Error) Error() string {
2525
// ClientError is connection error produced by this client,
2626
// i.e. connection failures or timeouts.
2727
type ClientError struct {
28-
Code uint32
28+
Code CodeError
2929
Msg string
3030
}
3131

3232
// Error converts a ClientError to a string.
3333
func (clierr ClientError) Error() string {
34-
return fmt.Sprintf("%s (0x%x)", clierr.Msg, clierr.Code)
34+
return fmt.Sprintf("%s (0x%x)", clierr.Msg, uint32(clierr.Code))
3535
}
3636

3737
// Temporary returns true if next attempt to perform request may succeeded.
@@ -52,13 +52,22 @@ func (clierr ClientError) Temporary() bool {
5252
}
5353
}
5454

55+
// CodeError is an error providing code of failure, to distinct them using errors.Is.
56+
type CodeError uint32
57+
58+
// Error converts CodeError to a string.
59+
func (err CodeError) Error() string {
60+
return fmt.Sprintf("0x%x", uint32(err))
61+
}
62+
5563
// Tarantool client error codes.
5664
const (
57-
ErrConnectionNotReady = 0x4000 + iota
58-
ErrConnectionClosed = 0x4000 + iota
59-
ErrProtocolError = 0x4000 + iota
60-
ErrTimeouted = 0x4000 + iota
61-
ErrRateLimited = 0x4000 + iota
62-
ErrConnectionShutdown = 0x4000 + iota
63-
ErrIoError = 0x4000 + iota
65+
ErrConnectionNotReady CodeError = 0x4000 + iota
66+
ErrConnectionClosed
67+
ErrProtocolError
68+
ErrTimeouted
69+
ErrRateLimited
70+
ErrConnectionShutdown
71+
ErrIoError
72+
ErrCancelledCtx
6473
)

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func ExamplePingRequest_Context() {
167167
fmt.Println("Ping Error", regexp.MustCompile("[0-9]+").ReplaceAllString(err.Error(), "N"))
168168
// Output:
169169
// Ping Resp data []
170-
// Ping Error context is done (request ID N)
170+
// Ping Error context is done (request ID N) (NxN)
171171
}
172172

173173
func ExampleSelectRequest() {

tarantool_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tarantool_test
33
import (
44
"context"
55
"encoding/binary"
6+
"errors"
67
"fmt"
78
"io"
89
"log"
@@ -48,7 +49,7 @@ type Member struct {
4849
Val uint
4950
}
5051

51-
var contextDoneErrRegexp = regexp.MustCompile(`^context is done \(request ID [0-9]+\)$`)
52+
var contextDoneErrRegexp = regexp.MustCompile(fmt.Sprintf(`^context is done \(request ID [0-9]+\) \(%s\)$`, ErrCancelledCtx.Error()))
5253

5354
func (m *Member) EncodeMsgpack(e *msgpack.Encoder) error {
5455
if err := e.EncodeArrayLen(2); err != nil {
@@ -2737,6 +2738,8 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
27372738
if !contextDoneErrRegexp.Match([]byte(err.Error())) {
27382739
t.Fatalf("Failed to catch an error from done context")
27392740
}
2741+
// checking that we could use errors.Is to get known about error.
2742+
require.True(t, errors.Is(err, ErrCancelledCtx))
27402743
if resp != nil {
27412744
t.Fatalf("Response is not nil after the occurred error")
27422745
}
@@ -3298,6 +3301,7 @@ func TestClientIdRequestObjectWithPassedCanceledContext(t *testing.T) {
32983301
resp, err := conn.Do(req).Get()
32993302
require.Nilf(t, resp, "Response is empty")
33003303
require.NotNilf(t, err, "Error is not empty")
3304+
require.True(t, errors.Is(err, ErrCancelledCtx))
33013305
require.Regexp(t, contextDoneErrRegexp, err.Error())
33023306
}
33033307

0 commit comments

Comments
 (0)