Skip to content

Commit

Permalink
Errors should be pointers
Browse files Browse the repository at this point in the history
Avoid copying the error structure everytime by using pointers.

See #20
  • Loading branch information
rafaeljusto committed Mar 31, 2017
1 parent e06763b commit 339e7af
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 268 deletions.
8 changes: 4 additions & 4 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const encryptedLabel = "encrypted:"
//
// if causeErr, ok := err.(causer); ok {
// switch specificErr := causeErr.Cause().(type) {
// case archive.Error:
// case *archive.Error:
// // handle specifically
// case archive.PathError:
// case *archive.PathError:
// // handle specifically
// default:
// // unknown error
Expand Down Expand Up @@ -131,7 +131,7 @@ func build(tarArchive *tar.Writer, baseDir, source string) error {
//
// if causeErr, ok := err.(causer); ok {
// switch specificErr := causeErr.Cause().(type) {
// case archive.Error:
// case *archive.Error:
// // handle specifically
// default:
// // unknown error
Expand Down Expand Up @@ -201,7 +201,7 @@ func Encrypt(filename, secret string) (string, error) {
//
// if causeErr, ok := err.(causer); ok {
// switch specificErr := causeErr.Cause().(type) {
// case archive.Error:
// case *archive.Error:
// // handle specifically
// default:
// // unknown error
Expand Down
22 changes: 11 additions & 11 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestBuild(t *testing.T) {
backupPaths: func() []string {
return []string{"idontexist12345"}
}(),
expectedError: archive.PathError{
expectedError: &archive.PathError{
Path: "idontexist12345",
Code: archive.PathErrorCodeInfo,
Err: &os.PathError{
Expand All @@ -188,7 +188,7 @@ func TestBuild(t *testing.T) {

return []string{n}
}(),
expectedError: archive.PathError{
expectedError: &archive.PathError{
Path: path.Join(os.TempDir(), "toglacier-test-archive-dir-noperm"),
Code: archive.PathErrorCodeInfo,
Err: &os.PathError{
Expand All @@ -214,7 +214,7 @@ func TestBuild(t *testing.T) {

return []string{n}
}(),
expectedError: archive.PathError{
expectedError: &archive.PathError{
Path: path.Join(os.TempDir(), "toglacier-test-archive-file-noperm"),
Code: archive.PathErrorCodeOpeningFile,
Err: &os.PathError{
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestBuild(t *testing.T) {

return []string{n}
}(),
expectedError: archive.PathError{
expectedError: &archive.PathError{
Path: path.Join(os.TempDir(), "toglacier-test-archive-dir-file-noperm", "file1"),
Code: archive.PathErrorCodeOpeningFile,
Err: &os.PathError{
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestEncrypt(t *testing.T) {
description: "it should detect when it tries to encrypt a file that doesn't exist",
filename: "toglacier-idontexist.tmp",
secret: "12345678901234567890123456789012",
expectedError: archive.Error{
expectedError: &archive.Error{
Filename: "toglacier-idontexist.tmp",
Code: archive.ErrorCodeOpeningFile,
Err: &os.PathError{
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestEncrypt(t *testing.T) {
}(),
secret: "12345678901234567890123456789012",
randomSource: rand.Reader,
expectedError: archive.Error{
expectedError: &archive.Error{
Filename: path.Join(os.TempDir(), "toglacier-test-noperm"),
Code: archive.ErrorCodeOpeningFile,
Err: &os.PathError{
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestEncrypt(t *testing.T) {
return 0, errors.New("random error")
},
}
scenario.expectedError = archive.Error{
scenario.expectedError = &archive.Error{
Filename: f.Name(),
Code: archive.ErrorCodeGenerateRandomNumbers,
Err: errors.New("random error"),
Expand All @@ -368,7 +368,7 @@ func TestEncrypt(t *testing.T) {
scenario.secret = "123456"
scenario.randomSource = rand.Reader

scenario.expectedError = archive.Error{
scenario.expectedError = &archive.Error{
Filename: f.Name(),
Code: archive.ErrorCodeInitCipher,
Err: aes.KeySizeError(6),
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestDecrypt(t *testing.T) {
return n
}(),
secret: "12345678901234567890123456789012",
expectedError: archive.Error{
expectedError: &archive.Error{
Filename: path.Join(os.TempDir(), "toglacier-test-noperm"),
Code: archive.ErrorCodeOpeningFile,
Err: &os.PathError{
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestDecrypt(t *testing.T) {
scenario.encryptedFilename = f.Name()
scenario.secret = "123456"

scenario.expectedError = archive.Error{
scenario.expectedError = &archive.Error{
Filename: f.Name(),
Code: archive.ErrorCodeInitCipher,
Err: aes.KeySizeError(6),
Expand All @@ -501,7 +501,7 @@ func TestDecrypt(t *testing.T) {
f.Write(content)
return f.Name()
}(),
expectedError: archive.Error{
expectedError: &archive.Error{
Code: archive.ErrorCodeAuthFailed,
},
},
Expand Down
16 changes: 8 additions & 8 deletions internal/archive/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ type Error struct {
Err error
}

func newError(filename string, code ErrorCode, err error) Error {
return Error{
func newError(filename string, code ErrorCode, err error) *Error {
return &Error{
Filename: filename,
Code: code,
Err: errors.WithStack(err),
Expand Down Expand Up @@ -159,8 +159,8 @@ func ErrorEqual(first, second error) bool {
return first == second
}

err1, ok1 := errors.Cause(first).(Error)
err2, ok2 := errors.Cause(second).(Error)
err1, ok1 := errors.Cause(first).(*Error)
err2, ok2 := errors.Cause(second).(*Error)

if !ok1 || !ok2 {
return false
Expand Down Expand Up @@ -229,8 +229,8 @@ type PathError struct {
Err error
}

func newPathError(path string, code PathErrorCode, err error) PathError {
return PathError{
func newPathError(path string, code PathErrorCode, err error) *PathError {
return &PathError{
Path: path,
Code: code,
Err: errors.WithStack(err),
Expand Down Expand Up @@ -264,8 +264,8 @@ func PathErrorEqual(first, second error) bool {
return first == second
}

err1, ok1 := errors.Cause(first).(PathError)
err2, ok2 := errors.Cause(second).(PathError)
err1, ok1 := errors.Cause(first).(*PathError)
err2, ok2 := errors.Cause(second).(*PathError)

if !ok1 || !ok2 {
return false
Expand Down

0 comments on commit 339e7af

Please sign in to comment.