Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FIX BUG in error and WIP ADD tests

  • Loading branch information...
commit 952b5c734938bc2f9a1316d17d42654510240c10 1 parent e13f256
@alphazero authored
View
13 connection.go
@@ -173,12 +173,10 @@ func newConnHdl(spec *ConnectionSpec) (hdl *connHdl, err Error) {
switch {
case e != nil:
err = NewErrorWithCause(SYSTEM_ERR, fmt.Sprintf("%s(): could not open connection", here), e)
- log.Println(err.Message())
- return nil, err
+ return nil, withError(err)
case conn == nil:
err = NewError(SYSTEM_ERR, fmt.Sprintf("%s(): net.Dial returned nil, nil (?)", here))
- log.Println(err.Message())
- return nil, err
+ return nil, withError(err)
default:
configureConn(conn, spec)
hdl.spec = spec
@@ -208,14 +206,14 @@ func (c *connHdl) onConnect() (e Error) {
if c.spec.password != DefaultRedisPassword {
_, e = c.ServiceRequest(&AUTH, [][]byte{[]byte(c.spec.password)})
if e != nil {
- log.Printf("Error on AUTH - e:%s", e.Message())
+ log.Printf("<ERROR> Authentication failed - %s", e.Message())
return
}
}
if c.spec.db != DefaultRedisDB {
_, e = c.ServiceRequest(&SELECT, [][]byte{[]byte(fmt.Sprintf("%d", c.spec.db))})
if e != nil {
- log.Printf("Error on SELECT - e:%s", e.Message())
+ log.Printf("<ERROR> DB Select failed - %s", e.Message())
return
}
}
@@ -253,11 +251,10 @@ type SyncConnection interface {
func NewSyncConnection(spec *ConnectionSpec) (c SyncConnection, err Error) {
connHdl, e := newConnHdl(spec)
if e != nil {
- log.Println("newConnHdl failed: " + e.Message())
return nil, e
}
- connHdl.onConnect()
+ e = connHdl.onConnect()
return connHdl, e
}
View
49 error.go
@@ -44,6 +44,7 @@ const (
REDIS_ERR
SYSTEM_ERR
)
+
func (ec ErrorCategory) String() (s string) {
switch ec {
case SYSTEM_ERR:
@@ -53,14 +54,30 @@ func (ec ErrorCategory) String() (s string) {
}
return
}
+
// Defines the interfce to get details of an Error.
//
type Error interface {
+ // underlying error that caused this error - or nil
Cause() error
+
+ // support std. Go Error interface
Error() string
+
+ // category of error -- never nil
Category() ErrorCategory
+
+ // the error message - if a Redis error these are exactly
+ // as provided by the server e.g. "ERR - invalid password"
+ // possibly "" if SYSTEM-ERR
Message() string
+
+ // conv to String
String() string
+
+ // check if error is a Redis error e.g. "ERR - invalid password"
+ // a convenience method
+ IsRedisError() bool
}
type redisError struct {
msg string
@@ -68,6 +85,7 @@ type redisError struct {
cause error
}
+func (e redisError) IsRedisError() bool { return e.category == REDIS_ERR}
func (e redisError) Cause() error { return e.cause }
func (e redisError) Error() string { return e.String() }
func (e redisError) Category() ErrorCategory { return e.category }
@@ -120,17 +138,29 @@ func NewErrorWithCause(cat ErrorCategory, msg string, cause error) Error {
return e
}
-// ----------------
-// impl. utils
-//
-// a utility function for various components
-//
+
+// utility function emits log if _debug flag /debug() is true
+// Error is returned.
+// usage:
+// foo, e := FooBar()
+// if e != nil {
+// return withError(e)
+// }
func withError(e Error) Error {
if debug() {
log.Println(e)
}
return e
}
+
+// creates a new generic (Go) error
+// and emits log if _debug flag /debug() is true
+// Error is returned.
+// usage:
+// v := FooBar()
+// if v != expected {
+// return withNewError("value v is unexpected")
+// }
func withNewError(m string) error {
e := errors.New(m)
if debug() {
@@ -138,6 +168,15 @@ func withNewError(m string) error {
}
return e
}
+
+// creates a new redis.Error of category SYSTEM_ERR
+// and emits log if _debug flag /debug() is true
+// Error is returned.
+// usage:
+// _, e := SomeLibraryOrGoCall()
+// if e != nil {
+// return withNewError("value v is unexpected", e)
+// }
func withOsError(m string, cause error) error {
return withNewError(fmt.Sprintf("%s [cause: %s]", m, cause))
}
View
18 error_test.go
@@ -27,16 +27,16 @@ func testspec_et() tspec_et {
func commonErrorTest(t *testing.T, e Error, category ErrorCategory) {
if e.Error() == "" {
- t.Error ("BUG: nil results for e.Error()")
+ t.Error("BUG: nil results for e.Error()")
}
if e.Message() == "" {
- t.Error ("BUG: nil results for e.Message()")
+ t.Error("BUG: nil results for e.Message()")
}
if e.String() == "" {
- t.Error ("BUG: nil results for e.String()")
+ t.Error("BUG: nil results for e.String()")
}
if e.Category() != category {
- t.Errorf ("BUG: category not set correctly (exp:%s | got:%s)", category, e.Category())
+ t.Errorf("BUG: category not set correctly (exp:%s | got:%s)", category, e.Category())
}
}
@@ -59,6 +59,10 @@ func TestNewRedisError(t *testing.T) {
spec := testspec_et()
e := NewRedisError(spec.msg)
commonErrorTest(t, e, REDIS_ERR)
+
+ if !e.IsRedisError() {
+ t.Error("BUG: IsRedisError")
+ }
}
func TestNewSystemError(t *testing.T) {
@@ -66,13 +70,17 @@ func TestNewSystemError(t *testing.T) {
spec := testspec_et()
e := NewSystemError(spec.msg)
commonErrorTest(t, e, SYSTEM_ERR)
+
+ if e.IsRedisError() {
+ t.Error("BUG: IsRedisError")
+ }
}
func TestNewErrorWithCause(t *testing.T) {
spec := testspec_et()
- var cause simpleError;
+ var cause simpleError
e := NewErrorWithCause(spec.category, spec.msg, cause)
commonErrorTest(t, e, spec.category)
View
10 redis.go
@@ -94,7 +94,7 @@ package redis
import (
"flag"
- // "runtime";
+ "runtime";
)
// Common interface supported by all clients
@@ -518,8 +518,8 @@ type AsyncClient interface {
// for this package are all prefixed by "redis:" to prevent possible name collisions.
//
func init() {
- // runtime.GOMAXPROCS(2);
- // flag.Parse();
+ runtime.GOMAXPROCS(2);
+ flag.Parse();
}
// redis:d
@@ -527,4 +527,6 @@ func init() {
// global debug flag for redis package components.
//
var _debug *bool = flag.Bool("redis:d", false, "debug flag for go-redis") // TEMP: should default to false
-func debug() bool { return *_debug }
+func debug() bool {
+ return *_debug
+}
View
7 synchclient.go
@@ -51,12 +51,11 @@ func NewSynchClient() (c Client, err Error) {
//
func NewSynchClientWithSpec(spec *ConnectionSpec) (c Client, err Error) {
_c := new(syncClient)
- conn, err := NewSyncConnection(spec)
+ _c.conn, err = NewSyncConnection(spec)
if err != nil {
- log.Println("NewSyncConnection() raised error: ", err.Message())
- return nil, err
+ return nil, withError (err)
}
- _c.conn = conn
+// _c.conn = conn
return _c, nil
}
View
19 synchclient_test.go
@@ -13,6 +13,8 @@ type tspec_sct struct {
connspec *ConnectionSpec
}
+// default values for test
+//
func testspec_sct() tspec_sct {
var spec tspec_sct
@@ -25,16 +27,29 @@ func testspec_sct() tspec_sct {
return spec
}
+func TestClientConnectWithBadSpec(t *testing.T) {
+ spec := testspec_sct()
+ spec.connspec.Password("bad-password")
+ c, err := NewSynchClientWithSpec(spec.connspec)
+ if err == nil {
+ t.Error("BUG: Expected a RedisError")
+ }
+ if c != nil {
+ t.Error("BUG: client reference expected to be nil")
+ }
+}
+
func TestClientConnectWithSpec(t *testing.T) {
spec := testspec_sct()
c, err := NewSynchClientWithSpec(spec.connspec)
if err != nil {
- t.Error("failed to create client with spec. Error: %s", err.Message())
+ t.Error("failed to create client with spec. Error: ", err.Message())
} else if c == nil {
- t.Error("failed to create client with spec. Error: %s")
+ t.Error("BUG: client is nil")
}
}
+
func TestEnd_sct(t *testing.T) {
log.Println("synchclient test")
}
Please sign in to comment.
Something went wrong with that request. Please try again.