-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
errors/errorsutil
: add ErrorWithLocation{}
, `NewErrorWithLo…
…cation()`
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package errorsutil | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
// ErrorWithLocation represents an error message with code file and line locaiton. | ||
// It is automatically populated when instantiated with `NewErrorWithLocation()`, | ||
// and follows the `errors.Error` interface. | ||
type ErrorWithLocation struct { | ||
Msg string | ||
File string | ||
Line int | ||
} | ||
|
||
func (e *ErrorWithLocation) Error() string { | ||
return fmt.Sprintf("%s (at %s:%d)", e.Msg, e.File, e.Line) | ||
} | ||
|
||
func NewErrorWithLocation(msg string) error { | ||
_, file, line, ok := runtime.Caller(1) | ||
if !ok { | ||
file = "unknown" | ||
line = -1 | ||
} | ||
return &ErrorWithLocation{ | ||
Msg: msg, | ||
File: file, | ||
Line: line, | ||
} | ||
} |
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,34 @@ | ||
package errorsutil | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var errorWithLocationTests = []struct { | ||
fn func() error | ||
errMsgSuffix string | ||
}{ | ||
{func() error { _, err := time.Parse(time.RFC3339, "Mon, 2024-12-31"); return err }, "github.com/grokify/mogo/errors/errorsutil/error_with_location_test.go:22"}, | ||
} | ||
|
||
func TestErrorWithLocation(t *testing.T) { | ||
for _, tt := range errorWithLocationTests { | ||
try := tt.fn() | ||
if try == nil { | ||
panic("no error") | ||
} | ||
tryWithLocation := NewErrorWithLocation(try.Error()) | ||
idx, idxCalc, ok := isSuffixOnly(tryWithLocation.Error(), tt.errMsgSuffix) | ||
if !ok { | ||
t.Errorf("errorsutil.NewErrorWithLocation(\"%s\"): mismatch want suffix (%s) got (%s), idx (%d) idxCalc (%d)", tryWithLocation.Error(), tt.errMsgSuffix, tryWithLocation.Error(), idx, idxCalc) | ||
} | ||
} | ||
} | ||
|
||
func isSuffixOnly(s, substr string) (int, int, bool) { | ||
idx := strings.Index(s, substr) | ||
idxCalc := len(s) - len(substr) - 1 | ||
return idx, idxCalc, idx == idxCalc | ||
} |