-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TraceWithLocation and revamps docs
- Loading branch information
Showing
6 changed files
with
217 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package terr_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alnvdl/terr" | ||
) | ||
|
||
// This example shows how to call different terr functions and print a traced | ||
// error tree at the end. | ||
func Example() { | ||
err := terr.Newf("base") | ||
traced := terr.Trace(err) | ||
wrapped := terr.Newf("wrapped: %w", traced) | ||
masked := terr.Newf("masked: %v", wrapped) | ||
fmt.Printf("%@\n", masked) | ||
} |
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,29 @@ | ||
package terr_test | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/alnvdl/terr" | ||
) | ||
|
||
type ValidationError struct{ msg string } | ||
|
||
func (e *ValidationError) Error() string { | ||
return e.msg | ||
} | ||
|
||
func NewValidationError(msg string) error { | ||
// Custom errors can define a location for the error. In this case, we set | ||
// it to the location of the caller of this function. | ||
_, file, line, _ := runtime.Caller(1) | ||
return terr.TraceWithLocation(&ValidationError{msg}, file, line) | ||
} | ||
|
||
// This example shows how to adding tracing information to custom error types | ||
// using TraceWithLocation. | ||
func ExampleTraceWithLocation() { | ||
// err will be annotated with the line number of the next line. | ||
err := NewValidationError("x must be >= 0") | ||
fmt.Printf("%@\n", err) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
module github.com/alnvdl/terr | ||
|
||
retract v1.0.0 // Published with go 1.13 directive. | ||
retract v1.0.1 // Published with README typo, v1.0.0 retract did not work. | ||
retract [v0.0.0, v1.0.7] // Incomplete and/or wrong releases. | ||
|
||
go 1.20 |
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.