Throw is a Go library that provides a simple way to build typed errors with attributes.
Throw's API provides a set of chainable methods that allow you to easily build errors with attributes. It also provides a set of predefined error types that you can use to build your errors.
Throw is inspired by the awesome rs/zerolog library.
go get github.com/pauloRohling/throw
- Chainable methods for building errors with attributes.
- Predefined error types for common error types.
- Support for standard and advanced attributes.
Str
Int
,Int8
,Int16
,Int32
,Int64
Uint
,Uint8
,Uint16
,Uint32
,Uint64
Float32
,Float64
Any
: Formats any type using the%+v
format.Json
: Formats any type as json using thejson.Marshal
function.
Please refer to the examples for more information on how to use Throw.
Throw provides a helper function to assert that the error is of the expected type in a more idiomatic way.
package fail
import "github.com/pauloRohling/throw"
func AlwaysFail() error {
return throw.NewTypedErrorBuilder("MyCustomError").
Str("reason", "something went wrong").
Int("code", 500).
Msg("An error has occurred!")
}
func TestAlwaysFail(t *testing.T) {
err := AlwaysFail()
throw.AssertType(t, err, "MyCustomError")
}
Throw is released under the MIT License.