Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorsMap ¶
ErrorsMap is a map of error slices representing field-specific error messages.
Example ¶
package main
import (
"fmt"
"github.com/markbates/errs"
)
func main() {
// Create an ErrorsMap error with field-specific errors.
err := errs.ErrorsMap{
"server": {errs.String("error2"), errs.String("error1")},
"database": {errs.String("error3")},
}
// Error printing is sorted by field name,
// and each error is printed on its own line.
// The messages for each field are also sorted.
fmt.Println(err.Error())
}
Output: database: - error3 server: - error1 - error2
type Int ¶
type Int int
Int is a typed integer error value.
Example ¶
package main
import (
"fmt"
"github.com/markbates/errs"
)
func main() {
// Create an Int error with a specific value.
err := errs.Int(42)
fmt.Println(err.Error())
// Create an Int error with the zero value.
err = errs.Int(0)
fmt.Println(err.Error())
}
Output: 42 error 0 error
type StatusCode ¶
type StatusCode int
StatusCode is a typed error value that represents an HTTP status code.
Example ¶
package main
import (
"fmt"
"github.com/markbates/errs"
)
func main() {
// Create a StatusCode error with a specific code.
err := errs.StatusCode(404)
fmt.Println(err.StatusCode())
fmt.Println(err.Error())
// Create a StatusCode error with the zero value, which defaults to 200.
err = errs.StatusCode(0)
fmt.Println(err.StatusCode())
fmt.Println(err.Error())
}
Output: 404 status: 404 200 status: 200
func (StatusCode) Error ¶
func (s StatusCode) Error() string
Error formats the status code as "status: <code>".
func (StatusCode) Is ¶
func (s StatusCode) Is(target error) bool
Is reports whether the target is also a StatusCode error, ignoring the value.
func (StatusCode) MarshalJSON ¶
func (s StatusCode) MarshalJSON() ([]byte, error)
MarshalJSON marshals the numeric status code value.
func (StatusCode) StatusCode ¶
func (s StatusCode) StatusCode() int
StatusCode returns the code value, defaulting to 200 when it is zero.
type String ¶
type String string
String is a typed string error value.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/markbates/errs"
)
func main() {
err1 := errs.String("an error occurred")
fmt.Println(err1.Error())
err2 := errs.String("boom")
fmt.Println(err2.Error())
err3 := fmt.Errorf("wrapping: %w", err1)
fmt.Println(err3.Error())
fmt.Println(errors.Is(err3, err1))
}
Output: an error occurred boom wrapping: an error occurred true
type StringsMap ¶
StringsMap is a map of string slices representing field-specific error messages.
Example ¶
package main
import (
"fmt"
"github.com/markbates/errs"
)
func main() {
// Create a StringsMap error with field-specific errors.
err := errs.StringsMap{
"server": {"error2", "error1"},
"database": {"error3"},
}
// Error printing is sorted by field name,
// and each error is printed on its own line.
// The messages for each field are also sorted.
fmt.Println(err.Error())
}
Output: database: - error3 server: - error1 - error2
func (StringsMap) Error ¶
func (m StringsMap) Error() string
Error returns a formatted string representation of the StringsMap.
func (StringsMap) Is ¶
func (m StringsMap) Is(target error) bool
Is returns true if the target is a StringsMap.