Skip to content

v3: box.New should return an error instead of panic #448

Open
@bigbes

Description

@bigbes

In user-facing libraries, it's better to fail early with a clearer error message, rather than "invalid memory address or nil pointer dereference". While avoiding panics entirely in third-party libraries is ideal, since we can't break compatibility, this should be implemented in v3.

For example:

func New(conn tarantool.Doer) *Box {
	if conn == nil {
		// Check if the provided Tarantool connection is nil, and if it is, panic with an error
		// message. panic early helps to catch and fix nil pointer issues in the code.
		panic("tarantool connection cannot be nil")
	}

	return &Box{
		conn: conn, // Assigns the provided Tarantool connection.
	}
}

should be

func New(conn tarantool.Doer) (*Box, error) {
	if conn == nil {
		// Check if the provided Tarantool connection is nil, and if it is, panic with an error
		// message. panic early helps to catch and fix nil pointer issues in the code.
		return errors.New("tarantool connection cannot be nil")
	}

	return &Box{
		conn: conn, // Assigns the provided Tarantool connection.
	}, nil
}

func MustNew(conn tarantool.Doer) *Box {
	return utils.Must(New(conn))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions