when using errors.New (), report panic: runtime error: invalid memory address or nil pointer dereference
func (self *Control) Register(ctx context.Context, in *push.RegisterRequest) (*push.RegisterResponse, error) {
var err error
var res = &push.RegisterResponse{
Code:false,
Msg:"",
}
userInfo, err := service.CheckRegisterParams(ctx, in)
if err != nil{
res.Msg = err.Error()
return res, err
}
if service.IsBlackIp(ctx, in.UserIp){
res.Msg = ""
return res, errors.New("")//painc
return res, err//
}
err = service.Register(ctx, in.User, in.DeviceNo, in.Ip, in.Port, userInfo)
if err != nil{
res.Msg = err.Error()
return res, err
}
res.Code = true
res.Msg = "OK"
return res, nil
}
detailed error information is as follows:
--- FAIL: TestRegiser (0.01s)
session_test.go:70: fail, rpc error: code = Unknown desc =
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14730d0]
goroutine 5 [running]:
testing.tRunner.func1(0xc4201923c0)
/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:742 +0x29d
panic(0x15028e0, 0x18f2c40)
/usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
session/test.TestRegiser(0xc4201923c0)
/Users/xxxx/devsoft/go/src/session/test/session_test.go:72 +0x270
testing.tRunner(0xc4201923c0, 0x15d2e78)
/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x2e0