//
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
//
log.Printf("%s: checksum mismatch: %s (actual hash %s)\n",
vol, blockFilename, filehash)
- return buf, &KeepError{ErrCorrupt, errors.New("Corrupt")}
+ return buf, CorruptError
}
// Success!
}
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)
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
if bytes.Compare(block, oldblock) == 0 {
return nil
} else {
- return &KeepError{ErrCollision, errors.New("Collision")}
+ return CollisionError
}
}
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
}
}
// 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)
}
}
// 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)
}
}
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")
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")
// 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)
}
}
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)
}
}