Skip to content

Commit 71697b5

Browse files
authored
Merge branch 'master' into ermyar/gh-457_returning_ctx_cause_error
2 parents 506fb5f + cbcf6a4 commit 71697b5

File tree

7 files changed

+81
-38
lines changed

7 files changed

+81
-38
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ 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

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

23+
2124
### Fixed
2225

2326
## [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

box/tarantool_test.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func TestBox_Sugar_Info(t *testing.T) {
4848
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
4949
require.NoError(t, err)
5050

51-
info, err := box.New(conn).Info()
51+
b := box.MustNew(conn)
52+
53+
info, err := b.Info()
5254
require.NoError(t, err)
5355

5456
validateInfo(t, info)
@@ -83,7 +85,7 @@ func TestBox_Sugar_Schema_UserCreate_NoError(t *testing.T) {
8385
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
8486
require.NoError(t, err)
8587

86-
b := box.New(conn)
88+
b := box.MustNew(conn)
8789

8890
// Create new user.
8991
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -103,7 +105,7 @@ func TestBox_Sugar_Schema_UserCreate_CanConnectWithNewCred(t *testing.T) {
103105
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
104106
require.NoError(t, err)
105107

106-
b := box.New(conn)
108+
b := box.MustNew(conn)
107109

108110
// Create new user.
109111
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -137,7 +139,7 @@ func TestBox_Sugar_Schema_UserCreate_AlreadyExists(t *testing.T) {
137139
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
138140
require.NoError(t, err)
139141

140-
b := box.New(conn)
142+
b := box.MustNew(conn)
141143

142144
// Create new user.
143145
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -167,7 +169,7 @@ func TestBox_Sugar_Schema_UserCreate_ExistsTrue(t *testing.T) {
167169
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
168170
require.NoError(t, err)
169171

170-
b := box.New(conn)
172+
b := box.MustNew(conn)
171173

172174
// Create new user.
173175
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -193,7 +195,7 @@ func TestBox_Sugar_Schema_UserCreate_IfNotExistsNoErr(t *testing.T) {
193195
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
194196
require.NoError(t, err)
195197

196-
b := box.New(conn)
198+
b := box.MustNew(conn)
197199

198200
// Create new user.
199201
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -217,7 +219,7 @@ func TestBox_Sugar_Schema_UserPassword(t *testing.T) {
217219
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
218220
require.NoError(t, err)
219221

220-
b := box.New(conn)
222+
b := box.MustNew(conn)
221223

222224
// Require password hash.
223225
hash, err := b.Schema().User().Password(ctx, password)
@@ -236,7 +238,7 @@ func TestBox_Sugar_Schema_UserDrop_AfterCreate(t *testing.T) {
236238
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
237239
require.NoError(t, err)
238240

239-
b := box.New(conn)
241+
b := box.MustNew(conn)
240242

241243
// Create new user
242244
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -258,7 +260,7 @@ func TestBox_Sugar_Schema_UserDrop_DoubleDrop(t *testing.T) {
258260
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
259261
require.NoError(t, err)
260262

261-
b := box.New(conn)
263+
b := box.MustNew(conn)
262264

263265
// Create new user
264266
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
@@ -286,7 +288,7 @@ func TestBox_Sugar_Schema_UserDrop_UnknownUser(t *testing.T) {
286288
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
287289
require.NoError(t, err)
288290

289-
b := box.New(conn)
291+
b := box.MustNew(conn)
290292

291293
// Require error cause user not exists
292294
err = b.Schema().User().Drop(ctx, "some_strange_not_existing_name", box.UserDropOptions{})
@@ -305,7 +307,7 @@ func TestSchemaUser_Passwd_NotFound(t *testing.T) {
305307
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
306308
require.NoError(t, err)
307309

308-
b := box.New(conn)
310+
b := box.MustNew(conn)
309311

310312
err = b.Schema().User().Passwd(ctx, "not-exists-passwd", "new_password")
311313
require.Error(t, err)
@@ -329,7 +331,7 @@ func TestSchemaUser_Passwd_Ok(t *testing.T) {
329331
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
330332
require.NoError(t, err)
331333

332-
b := box.New(conn)
334+
b := box.MustNew(conn)
333335

334336
// New user change password and connect
335337

@@ -367,7 +369,7 @@ func TestSchemaUser_Passwd_WithoutGrants(t *testing.T) {
367369
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
368370
require.NoError(t, err)
369371

370-
b := box.New(conn)
372+
b := box.MustNew(conn)
371373

372374
err = b.Schema().User().Create(ctx, username,
373375
box.UserCreateOptions{Password: startPassword, IfNotExists: true})
@@ -381,7 +383,8 @@ func TestSchemaUser_Passwd_WithoutGrants(t *testing.T) {
381383
require.NoError(t, err)
382384
require.NotNil(t, conn2Fail)
383385

384-
bFail := box.New(conn2Fail)
386+
bFail := box.MustNew(conn2Fail)
387+
385388
// can't change self user password without grants
386389
err = bFail.Schema().User().Passwd(ctx, endPassword)
387390
require.Error(t, err)
@@ -401,7 +404,7 @@ func TestSchemaUser_Info_TestUserCorrect(t *testing.T) {
401404
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
402405
require.NoError(t, err)
403406

404-
b := box.New(conn)
407+
b := box.MustNew(conn)
405408

406409
privileges, err := b.Schema().User().Info(ctx, dialer.User)
407410
require.NoError(t, err)
@@ -418,7 +421,7 @@ func TestSchemaUser_Info_NonExistsUser(t *testing.T) {
418421
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
419422
require.NoError(t, err)
420423

421-
b := box.New(conn)
424+
b := box.MustNew(conn)
422425

423426
privileges, err := b.Schema().User().Info(ctx, "non-existing")
424427
require.Error(t, err)
@@ -438,7 +441,7 @@ func TestBox_Sugar_Schema_UserGrant_NoSu(t *testing.T) {
438441
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
439442
require.NoError(t, err)
440443

441-
b := box.New(conn)
444+
b := box.MustNew(conn)
442445

443446
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
444447
require.NoError(t, err)
@@ -471,7 +474,7 @@ func TestBox_Sugar_Schema_UserGrant_WithSu(t *testing.T) {
471474
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
472475
require.NoError(t, err)
473476

474-
b := box.New(conn)
477+
b := box.MustNew(conn)
475478

476479
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
477480
require.NoError(t, err)
@@ -521,7 +524,7 @@ func TestSchemaUser_Revoke_WithoutSu(t *testing.T) {
521524
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
522525
require.NoError(t, err)
523526

524-
b := box.New(conn)
527+
b := box.MustNew(conn)
525528

526529
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
527530
require.NoError(t, err)
@@ -555,7 +558,7 @@ func TestSchemaUser_Revoke_WithSu(t *testing.T) {
555558
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
556559
require.NoError(t, err)
557560

558-
b := box.New(conn)
561+
b := box.MustNew(conn)
559562

560563
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
561564
require.NoError(t, err)
@@ -603,7 +606,7 @@ func TestSchemaUser_Revoke_NonExistsPermission(t *testing.T) {
603606
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
604607
require.NoError(t, err)
605608

606-
b := box.New(conn)
609+
b := box.MustNew(conn)
607610

608611
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
609612
require.NoError(t, err)
@@ -639,7 +642,7 @@ func TestSession_Su_AdminPermissions(t *testing.T) {
639642
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
640643
require.NoError(t, err)
641644

642-
b := box.New(conn)
645+
b := box.MustNew(conn)
643646

644647
err = b.Session().Su(ctx, "admin")
645648
require.NoError(t, err)
@@ -652,7 +655,8 @@ func cleanupUser(username string) {
652655
log.Fatal(err)
653656
}
654657

655-
b := box.New(conn)
658+
b := box.MustNew(conn)
659+
656660
err = b.Schema().User().Drop(ctx, username, box.UserDropOptions{})
657661
if err != nil {
658662
log.Fatal(err)

0 commit comments

Comments
 (0)