Skip to content

Commit bf849e1

Browse files
authored
Merge branch 'master' into ermyar/gh-457_returning_ctx_cause_error
2 parents 73be5a7 + cbcf6a4 commit bf849e1

File tree

7 files changed

+83
-41
lines changed

7 files changed

+83
-41
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1111
### Added
1212

1313
* New types for MessagePack extensions compatible with go-option (#459).
14+
* Added `box.MustNew` wrapper for `box.New` without an error (#448).
1415

1516
### Changed
1617

17-
- Required Go version is `1.24` now (#456).
18-
- Now cases of `<-ctx.Done()` returns wrapped error provided by `ctx.Cause()`.
18+
* Required Go version is `1.24` now (#456).
19+
* `box.New` returns an error instead of panic (#448).
20+
* Now cases of `<-ctx.Done()` returns wrapped error provided by `ctx.Cause()`.
1921
Allows you compare it using `errors.Is/As` (#457).
20-
22+
2123
### Fixed
2224

2325
## [v2.4.1] - 2025-10-16

MIGRATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ TODO
99
### <a id="major-changes-v3">Major changes</a>
1010

1111
* Required Go version is `1.24` now.
12+
* `box.New` returns an error instead of panic
13+
* Added `box.MustNew` wrapper for `box.New` without an error
1214

1315
## Migration from v1.x.x to v2.x.x
1416

box/box.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package box
22

33
import (
4+
"errors"
5+
46
"github.com/tarantool/go-tarantool/v3"
57
)
68

@@ -11,16 +13,24 @@ type Box struct {
1113
}
1214

1315
// New returns a new instance of the box structure, which implements the Box interface.
14-
func New(conn tarantool.Doer) *Box {
16+
func New(conn tarantool.Doer) (*Box, error) {
1517
if conn == nil {
16-
// Check if the provided Tarantool connection is nil, and if it is, panic with an error
17-
// message. panic early helps to catch and fix nil pointer issues in the code.
18-
panic("tarantool connection cannot be nil")
18+
return nil, errors.New("tarantool connection cannot be nil")
1919
}
2020

2121
return &Box{
2222
conn: conn, // Assigns the provided Tarantool connection.
23+
}, nil
24+
}
25+
26+
// MustNew returns a new instance of the box structure, which implements the Box interface.
27+
// It panics if conn == nil.
28+
func MustNew(conn tarantool.Doer) *Box {
29+
b, err := New(conn)
30+
if err != nil {
31+
panic(err)
2332
}
33+
return b
2434
}
2535

2636
// Schema returns a new Schema instance, providing access to schema-related operations.

box/box_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,31 @@ import (
1515
func TestNew(t *testing.T) {
1616
t.Parallel()
1717

18+
_, err := box.New(nil)
19+
require.Error(t, err)
20+
}
21+
22+
func TestMustNew(t *testing.T) {
23+
t.Parallel()
24+
1825
// Create a box instance with a nil connection. This should lead to a panic.
19-
require.Panics(t, func() { box.New(nil) })
26+
require.Panics(t, func() { box.MustNew(nil) })
27+
}
28+
29+
func TestMocked_BoxNew(t *testing.T) {
30+
t.Parallel()
31+
32+
mock := test_helpers.NewMockDoer(t,
33+
test_helpers.NewMockResponse(t, "valid"),
34+
)
35+
36+
b, err := box.New(&mock)
37+
require.NoError(t, err)
38+
require.NotNil(t, b)
39+
40+
assert.Len(t, mock.Requests, 0)
41+
b.Schema().User().Exists(box.NewInfoRequest().Ctx(), "")
42+
require.Len(t, mock.Requests, 1)
2043
}
2144

2245
func TestMocked_BoxInfo(t *testing.T) {
@@ -37,7 +60,7 @@ func TestMocked_BoxInfo(t *testing.T) {
3760
mock := test_helpers.NewMockDoer(t,
3861
test_helpers.NewMockResponse(t, data),
3962
)
40-
b := box.New(&mock)
63+
b := box.MustNew(&mock)
4164

4265
info, err := b.Info()
4366
require.NoError(t, err)
@@ -57,7 +80,7 @@ func TestMocked_BoxSchemaUserInfo(t *testing.T) {
5780
mock := test_helpers.NewMockDoer(t,
5881
test_helpers.NewMockResponse(t, data),
5982
)
60-
b := box.New(&mock)
83+
b := box.MustNew(&mock)
6184

6285
privs, err := b.Schema().User().Info(context.Background(), "username")
6386
require.NoError(t, err)
@@ -82,7 +105,7 @@ func TestMocked_BoxSessionSu(t *testing.T) {
82105
test_helpers.NewMockResponse(t, []interface{}{}),
83106
errors.New("user not found or supplied credentials are invalid"),
84107
)
85-
b := box.New(&mock)
108+
b := box.MustNew(&mock)
86109

87110
err := b.Session().Su(context.Background(), "admin")
88111
require.NoError(t, err)

box/example_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func ExampleBox_Info() {
4545

4646
// Or use simple Box implementation.
4747

48-
b := box.New(client)
48+
b := box.MustNew(client)
4949

5050
info, err := b.Info()
5151
if err != nil {
@@ -88,7 +88,8 @@ func ExampleSchemaUser_Exists() {
8888
}
8989

9090
// Or use simple User implementation.
91-
b := box.New(client)
91+
b := box.MustNew(client)
92+
9293
exists, err := b.Schema().User().Exists(ctx, "user")
9394
if err != nil {
9495
log.Fatalf("Failed get box schema user exists with error: %s", err)
@@ -120,7 +121,7 @@ func ExampleSchemaUser_Create() {
120121
}
121122

122123
// Create SchemaUser.
123-
schemaUser := box.New(client).Schema().User()
124+
schemaUser := box.MustNew(client).Schema().User()
124125

125126
// Create a new user.
126127
username := "new_user"
@@ -153,7 +154,7 @@ func ExampleSchemaUser_Drop() {
153154
}
154155

155156
// Create SchemaUser.
156-
schemaUser := box.New(client).Schema().User()
157+
schemaUser := box.MustNew(client).Schema().User()
157158

158159
// Drop an existing user.
159160
username := "new_user"
@@ -192,7 +193,7 @@ func ExampleSchemaUser_Password() {
192193
}
193194

194195
// Create SchemaUser.
195-
schemaUser := box.New(client).Schema().User()
196+
schemaUser := box.MustNew(client).Schema().User()
196197

197198
// Get the password hash.
198199
password := "my-password"
@@ -221,7 +222,7 @@ func ExampleSchemaUser_Info() {
221222
}
222223

223224
// Create SchemaUser.
224-
schemaUser := box.New(client).Schema().User()
225+
schemaUser := box.MustNew(client).Schema().User()
225226

226227
info, err := schemaUser.Info(ctx, "test")
227228
if err != nil {

box/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestBox_Session(t *testing.T) {
13-
b := box.New(th.Ptr(th.NewMockDoer(t)))
13+
b := box.MustNew(th.Ptr(th.NewMockDoer(t)))
1414
require.NotNil(t, b.Session())
1515
}
1616

0 commit comments

Comments
 (0)