goerrors

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: MIT Imports: 4 Imported by: 2

README

goerrors

A Go package for creating and handling structured errors with names.

Installation

go get github.com/HashemJaafar7/goerrors

Usage

package main

import "github.com/HashemJaafar7/goerrors"
func main(){
	// Create an error and get its name
	err := goerrors.Errorf("DatabaseError", "connection failed")
	name := goerrors.GetName(err)
	stack := goerrors.GetStack(err)
	fmt.Println(err.Error())
	fmt.Println(name)
	fmt.Println(stack)
	// Output:
	// DatabaseError : connection failed
	// DatabaseError
	// c:/Users/hashem/Desktop/libraries/goerrors/lib_test.go:24 +0x3f
	// C:/Program Files/Go/src/testing/run_example.go:63 +0x2b0
	// C:/Program Files/Go/src/testing/example.go:41 +0x125
	// C:/Program Files/Go/src/testing/testing.go:2144 +0x71b
	// _testmain.go:53 +0x9b
}

Features

  • Create errors with associated names
  • Extract error names from errors
  • Extract error stack trace from errors
  • Special handling for nil errors
  • Type-safe error name extraction

Documentation

For detailed documentation, visit pkg.go.dev

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Overview

Package goerrors provides utilities for creating and handling structured errors in Go.

The package allows creation of errors with associated names for better error identification and handling. It provides functions to create named errors and extract error names from existing errors and extract error stack

Example usage:

err := goerrors.Errorf("ValidationError", "invalid input: %s", "missing field")
name := goerrors.GetName(err) // Returns "ValidationError"
fmt.Println(err.Error())

Index

Examples

Constants

View Source
const (
	NIL                   string = "NIL"
	ErrorInvalidErrorType string = "ErrorInvalidErrorType"
)

Variables

This section is empty.

Functions

func Errorf

func Errorf(name string, format string, a ...any) error

Errorf creates a new error with a custom name, formatted message, and stack trace. It takes a name for the error, a format string, and optional arguments for formatting. The resulting error includes:

  • A custom name identifier
  • A formatted error message
  • A colorized stack trace where .go file references are highlighted in red

Parameters:

  • name: String identifier for the error type
  • format: Printf-style format string for the error message
  • a: Optional variadic arguments used in format string

Returns:

  • error: A new error instance containing the name, formatted message and stack trace
Example
package main

import (
	"fmt"

	"github.com/HashemJaafar7/goerrors"
)

func main() {
	// Create a new error with a name and message
	err := goerrors.Errorf("ValidationError", "invalid field %q", "email")
	fmt.Println(err)
}
Output:

ValidationError : invalid field "email"

func GetName

func GetName(err error) string

GetName extracts and returns the error name from the provided error. If the error is nil, it returns NIL. If the error is not of type *errorStruct, it returns ErrorInvalidErrorType.

Example
package main

import (
	"fmt"

	"github.com/HashemJaafar7/goerrors"
)

func main() {
	// Create an error and get its name
	err := goerrors.Errorf("DatabaseError", "connection failed")
	name := goerrors.GetName(err)
	stack := goerrors.GetStack(err)
	fmt.Println(err.Error())
	fmt.Println(name)
	fmt.Println(stack)
}
Output:

DatabaseError : connection failed
DatabaseError
goroutine 1 [running]:
runtime/debug.Stack()
        C:/Program Files/Go/src/runtime/debug/stack.go:26 +0x5e
github.com/HashemJaafar7/goerrors.Errorf({0x429b63, 0xd}, {0x42ae23, 0x11}, {0x0, 0x0, 0x0})
        c:/Users/hashem/Desktop/libraries/goerrors/lib.go:65 +0x57
github.com/HashemJaafar7/goerrors_test.ExampleGetName()
        c:/Users/hashem/Desktop/libraries/goerrors/lib_test.go:24 +0x3f
testing.runExample({{0x429fce, 0xe}, 0x436df0, {0x43658c, 0x358}, 0x0})
        C:/Program Files/Go/src/testing/run_example.go:63 +0x2b0
testing.runExamples(0x467240?, {0x567d40, 0x2, 0x2?})
        C:/Program Files/Go/src/testing/example.go:41 +0x125
testing.(*M).Run(0xc0000581e0)
        C:/Program Files/Go/src/testing/testing.go:2144 +0x71b
main.main()
        _testmain.go:53 +0x9b

func GetStack

func GetStack(err error) string

GetStack retrieves the stack trace from an error. Returns:

  • The stack trace string if the error is of type *errorStruct
  • "nil" if the error is nil
  • "invalid error type" if the error is not of type *errorStruct

This function is used internally to extract stack trace information from custom errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

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