Skip to content

Throw is a Go library that provides a simple way to build typed errors with attributes

License

Notifications You must be signed in to change notification settings

pauloRohling/throw

Repository files navigation

Throw

GoDoc Go Report Card license

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.

Installation

go get github.com/pauloRohling/throw

Features

  • Chainable methods for building errors with attributes.
  • Predefined error types for common error types.
  • Support for standard and advanced attributes.

Attributes

Standard Attributes

  • Str
  • Int, Int8, Int16, Int32, Int64
  • Uint, Uint8, Uint16, Uint32, Uint64
  • Float32, Float64

Advanced Attributes

  • Any: Formats any type using the %+v format.
  • Json: Formats any type as json using the json.Marshal function.

Examples

Please refer to the examples for more information on how to use Throw.

Testing

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")
}

License

Throw is released under the MIT License.

About

Throw is a Go library that provides a simple way to build typed errors with attributes

Topics

Resources

License

Stars

Watchers

Forks