Documentation
¶
Overview ¶
Package errors provides stacktrace-enriched error handling with error codes.
Errors created with NewWithCode capture the file, line number, and function name at the call site. They carry a numeric code (from the codes package) that maps to user-facing messages in multiple languages.
Use GetCaller to extract stacktrace information, and GetCode to retrieve the error code from any error in the chain. Use Compile to convert an error into a user-facing response with the appropriate HTTP status code and localized message.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCaller ¶
GetCaller returns the file, line number, message, and any error from the stacktrace. If the error is not a stacktrace type, it returns an error indicating the cast failure.
func GetCode ¶
GetCode extracts the error code from a stacktrace error. If the error is not a stacktrace type, it returns NoCode.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/codes"
"github.com/alpardfm/go-toolkit/errors"
)
func main() {
err := errors.NewWithCode(codes.CodeBadRequest, "bad request")
code := errors.GetCode(err)
fmt.Println(code)
// Non-toolkit errors return NoCode (MaxUint32)
plainErr := fmt.Errorf("plain error")
code2 := errors.GetCode(plainErr)
fmt.Println(code2)
}
Output: 1006 4294967295
func NewWithCode ¶
NewWithCode creates a new error with a stacktrace, error code, and formatted message.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/codes"
"github.com/alpardfm/go-toolkit/errors"
)
func main() {
// Create a coded error
err := errors.NewWithCode(codes.CodeInvalidValue, "invalid input: %s", "email is required")
fmt.Println(err)
// Get the code from the error
code := errors.GetCode(err)
fmt.Println(code)
}
Output: Error: invalid input: email is required 1000
Example (UnwrapChain) ¶
ExampleNewWithCode_unwrapChain demonstrates creating an error chain and traversing it with standard library errors.Is.
package main
import (
"fmt"
"io"
"strings"
stderrors "errors"
"github.com/alpardfm/go-toolkit/codes"
"github.com/alpardfm/go-toolkit/errors"
)
func main() {
// Create a sentinel error
sentinel := io.EOF
// Demonstrate that GetCode works on toolkit errors
err1 := errors.NewWithCode(codes.CodeBadRequest, "first error")
err2 := errors.NewWithCode(codes.CodeInvalidValue, "second error")
fmt.Println(errors.GetCode(err1))
fmt.Println(errors.GetCode(err2))
// Standard errors.Is works with sentinel errors
wrappedSentinel := fmt.Errorf("wrapped: %w", sentinel)
fmt.Println(stderrors.Is(wrappedSentinel, io.EOF))
// Verify the error message contains expected text
fmt.Println(strings.Contains(err1.Error(), "first error"))
}
Output: 1006 1000 true true
Types ¶
type App ¶
type App struct {
Code codes.Code `json:"code"`
Title string `json:"title"`
Body string `json:"body"`
// contains filtered or unexported fields
}
App represents an application-level error with a code, user-facing title and body, and an underlying system error.
func Compile ¶
Compile returns an error and creates new App errors
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/codes"
"github.com/alpardfm/go-toolkit/errors"
)
func main() {
err := errors.NewWithCode(codes.CodeAuthFailure, "token expired")
statusCode, appErr := errors.Compile(err, "en")
fmt.Println(statusCode)
fmt.Println(appErr.Code)
fmt.Println(appErr.Title)
}
Output: 401 1703 Unauthorized