Open
Description
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))
}