errors

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 7 Imported by: 2 modules, 18 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func GetCaller(err error) (string, int, string, error)

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

func GetCode(err error) codes.Code

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

func NewWithCode(code codes.Code, msg string, val ...any) error

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

func Compile(err error, lang string) (int, App)

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

func (*App) Error

func (e *App) Error() string

Error returns the string representation of the underlying system error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL