-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mismatching marker handling, add better error handling (#51)
* Add errorsplus for composite errors * Adjust error message * Change error message to human friendly message * Remove duplicated message * Add indentation keep support, and test case for align * Add proper error handling when markers don't match * Adjust error message * Add clarification comment * Add better error handling test case * Add test case for indent keep mode * Add test case for broken marker * Correct error message
- Loading branch information
Showing
18 changed files
with
357 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package errorsplus | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Errors represents a slice of errors. | ||
type Errors []error | ||
|
||
func (es Errors) Error() string { | ||
switch len(es) { | ||
case 0: | ||
return "" | ||
case 1: | ||
return fmt.Sprintf("%v", es[0]) | ||
} | ||
|
||
rt := "more than one error occurred:" | ||
for _, e := range es { | ||
if e != nil { | ||
rt = fmt.Sprintf("%s\n\t%v", rt, e) | ||
} | ||
} | ||
return rt | ||
} | ||
|
||
func (es Errors) Is(target error) bool { | ||
if len(es) == 0 { | ||
return false | ||
} | ||
|
||
for _, e := range es { | ||
if errors.Is(e, target) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package errorsplus_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/upsidr/importer/internal/errorsplus" | ||
) | ||
|
||
var ( | ||
errFirst = errors.New("first error") | ||
errSecond = errors.New("second error") | ||
errThird = errors.New("third error") | ||
errOther = errors.New("some other error") | ||
) | ||
|
||
func TestCompositeErrors(t *testing.T) { | ||
cases := map[string]struct { | ||
errs errorsplus.Errors | ||
wantErr error | ||
wantErrString string | ||
}{ | ||
"zero error": { | ||
errs: errorsplus.Errors{ | ||
// empty | ||
}, | ||
wantErr: nil, // cannot be checked against nil, this is skipped explicitly below | ||
wantErrString: "", // empty | ||
}, | ||
"single error": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
}, | ||
wantErr: errFirst, | ||
wantErrString: "first error", | ||
}, | ||
"2 errors": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
errSecond, | ||
}, | ||
wantErr: errFirst, | ||
wantErrString: `more than one error occurred: | ||
first error | ||
second error`, | ||
}, | ||
"3 errors": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
errSecond, | ||
errThird, | ||
}, | ||
wantErr: errFirst, | ||
wantErrString: `more than one error occurred: | ||
first error | ||
second error | ||
third error`, | ||
}, | ||
"duplicated errors": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
errFirst, | ||
errFirst, | ||
}, | ||
wantErr: errFirst, | ||
wantErrString: `more than one error occurred: | ||
first error | ||
first error | ||
first error`, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
if tc.wantErr != nil && !errors.Is(tc.errs, tc.wantErr) { | ||
t.Errorf("error mismatch\n\tcomposite error: %v\n\twanted error: %v", tc.errs, tc.wantErr) | ||
} | ||
|
||
errString := tc.errs.Error() | ||
if diff := cmp.Diff(tc.wantErrString, errString); diff != "" { | ||
t.Errorf("result didn't match (-want / +got)\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCompositeErrorsMismatch(t *testing.T) { | ||
cases := map[string]struct { | ||
errs errorsplus.Errors | ||
mismatchedErr error | ||
}{ | ||
"zero error": { | ||
errs: errorsplus.Errors{ | ||
// empty | ||
}, | ||
mismatchedErr: errOther, | ||
}, | ||
"single error": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
}, | ||
mismatchedErr: errOther, | ||
}, | ||
"2 errors": { | ||
errs: errorsplus.Errors{ | ||
errFirst, | ||
errSecond, | ||
}, | ||
mismatchedErr: errOther, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
if errors.Is(tc.errs, tc.mismatchedErr) { | ||
t.Errorf("unexpected error match, where it should not be matched with errors.Is\n\tcomposite error: %v\n\ttarget error: %v", tc.errs, tc.mismatchedErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.