Clean up error handling (refs #2292).
authorTim Pierce <twp@curoverse.com>
Sat, 12 Apr 2014 18:24:49 +0000 (14:24 -0400)
committerTim Pierce <twp@curoverse.com>
Sat, 12 Apr 2014 18:24:49 +0000 (14:24 -0400)
services/keep/keep.go
services/keep/keep_test.go

index bd9360f40298b57a2381299a25b9287f1d39b5c6..5113727169ffcd83a976d6074c402fe5be822fd1 100644 (file)
@@ -44,20 +44,21 @@ var KeepVolumes []string
 //
 type KeepError struct {
        HTTPCode int
-       Err      error
+       ErrMsg   string
 }
 
-const (
-       ErrCollision = 400
-       ErrMD5Fail   = 401
-       ErrCorrupt   = 402
-       ErrNotFound  = 404
-       ErrOther     = 500
-       ErrFull      = 503
+var (
+       CollisionError = &KeepError{400, "Collision"}
+       MD5Error       = &KeepError{401, "MD5 Failure"}
+       CorruptError   = &KeepError{402, "Corruption"}
+       NotFoundError  = &KeepError{404, "Not Found"}
+       GenericError   = &KeepError{500, "Fail"}
+       FullError      = &KeepError{503, "Full"}
+       TooLongError   = &KeepError{504, "Too Long"}
 )
 
 func (e *KeepError) Error() string {
-       return fmt.Sprintf("Error %d: %s", e.HTTPCode, e.Err.Error())
+       return e.ErrMsg
 }
 
 // This error is returned by ReadAtMost if the available
@@ -242,7 +243,7 @@ func GetBlock(hash string) ([]byte, error) {
                        //
                        log.Printf("%s: checksum mismatch: %s (actual hash %s)\n",
                                vol, blockFilename, filehash)
-                       return buf, &KeepError{ErrCorrupt, errors.New("Corrupt")}
+                       return buf, CorruptError
                }
 
                // Success!
@@ -250,7 +251,7 @@ func GetBlock(hash string) ([]byte, error) {
        }
 
        log.Printf("%s: not found on any volumes, giving up\n", hash)
-       return buf, &KeepError{ErrNotFound, errors.New("not found: " + hash)}
+       return buf, NotFoundError
 }
 
 /* PutBlock(block, hash)
@@ -284,7 +285,7 @@ func PutBlock(block []byte, hash string) error {
        blockhash := fmt.Sprintf("%x", md5.Sum(block))
        if blockhash != hash {
                log.Printf("%s: MD5 checksum %s did not match request", hash, blockhash)
-               return &KeepError{ErrMD5Fail, errors.New("MD5Fail")}
+               return MD5Error
        }
 
        // If we already have a block on disk under this identifier, return
@@ -296,7 +297,7 @@ func PutBlock(block []byte, hash string) error {
                if bytes.Compare(block, oldblock) == 0 {
                        return nil
                } else {
-                       return &KeepError{ErrCollision, errors.New("Collision")}
+                       return CollisionError
                }
        }
 
@@ -340,10 +341,10 @@ func PutBlock(block []byte, hash string) error {
 
        if allFull {
                log.Printf("all Keep volumes full")
-               return &KeepError{ErrFull, errors.New("Full")}
+               return FullError
        } else {
                log.Printf("all Keep volumes failed")
-               return &KeepError{ErrOther, errors.New("Fail")}
+               return GenericError
        }
 }
 
index c1f4e658958755704c28ee1690261bce39372d7b..348445e78d105a79a136ea010c6139aad9ae4beb 100644 (file)
@@ -62,13 +62,8 @@ func TestGetBlockMissing(t *testing.T) {
 
        // Check that GetBlock returns failure.
        result, err := GetBlock(TEST_HASH)
-       if err == nil {
-               t.Errorf("GetBlock incorrectly returned success: ", result)
-       } else {
-               ke := err.(*KeepError)
-               if ke.HTTPCode != ErrNotFound {
-                       t.Errorf("GetBlock: %v", ke)
-               }
+       if err != NotFoundError {
+               t.Errorf("Expected NotFoundError, got %v", result)
        }
 }
 
@@ -88,8 +83,8 @@ func TestGetBlockCorrupt(t *testing.T) {
 
        // Check that GetBlock returns failure.
        result, err := GetBlock(TEST_HASH)
-       if err == nil {
-               t.Errorf("GetBlock incorrectly returned success: %s", result)
+       if err != CorruptError {
+               t.Errorf("Expected CorruptError, got %v", result)
        }
 }
 
@@ -113,7 +108,7 @@ func TestPutBlockOK(t *testing.T) {
 
        result, err := GetBlock(TEST_HASH)
        if err != nil {
-               t.Fatalf("GetBlock: %s", err.Error())
+               t.Fatalf("GetBlock returned error: %v", err)
        }
        if string(result) != string(TEST_BLOCK) {
                t.Error("PutBlock/GetBlock mismatch")
@@ -140,7 +135,7 @@ func TestPutBlockOneVol(t *testing.T) {
 
        result, err := GetBlock(TEST_HASH)
        if err != nil {
-               t.Fatalf("GetBlock: %s", err.Error())
+               t.Fatalf("GetBlock: %v", err)
        }
        if string(result) != string(TEST_BLOCK) {
                t.Error("PutBlock/GetBlock mismatch")
@@ -161,19 +156,14 @@ func TestPutBlockMD5Fail(t *testing.T) {
 
        // Check that PutBlock returns the expected error when the hash does
        // not match the block.
-       if err := PutBlock(BAD_BLOCK, TEST_HASH); err == nil {
-               t.Error("PutBlock succeeded despite a block mismatch")
-       } else {
-               ke := err.(*KeepError)
-               if ke.HTTPCode != ErrMD5Fail {
-                       t.Errorf("PutBlock returned the wrong error (%v)", ke)
-               }
+       if err := PutBlock(BAD_BLOCK, TEST_HASH); err != MD5Error {
+               t.Error("Expected MD5Error, got %v", err)
        }
 
        // Confirm that GetBlock fails to return anything.
-       if result, err := GetBlock(TEST_HASH); err == nil {
-               t.Errorf("GetBlock succeded after a corrupt block store, returned '%s'",
-                       string(result))
+       if result, err := GetBlock(TEST_HASH); err != NotFoundError {
+               t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
+                       string(result), err)
        }
 }
 
@@ -220,7 +210,7 @@ func TestPutBlockCollision(t *testing.T) {
 
        if err := PutBlock(b2, locator); err == nil {
                t.Error("PutBlock did not report a collision")
-       } else if err.(*KeepError).HTTPCode != ErrCollision {
+       } else if err != CollisionError {
                t.Errorf("PutBlock returned %v", err)
        }
 }