errors

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 2 Imported by: 0

README

Auth Errors

Overview

The Auth Errors package provides specialized error handling for the authentication and authorization components. It extends the core errors package with auth-specific error types and utilities.

Features

  • Auth-Specific Errors: Specialized error types for authentication and authorization failures
  • Context Information: Add and retrieve context information from errors
  • Operation Tracking: Track the operation that caused an error
  • Error Wrapping: Wrap errors with additional information
  • Error Inspection: Extract information from errors

Installation

go get github.com/abitofhelp/servicelib/auth/errors

Quick Start

See the Error Handling example for a complete, runnable example of how to use the auth errors package.

API Documentation

Core Types
AuthError

The main error type for authentication and authorization errors.

type AuthError struct {
    // Fields
}
Key Methods
NewAuthError

Creates a new auth error.

func NewAuthError(err error, op string, message string, context map[string]interface{}) *AuthError
WithContext

Adds context information to an error.

func WithContext(err error, key string, value interface{}) error
WithOp

Adds operation information to an error.

func WithOp(err error, op string) error
WithMessage

Adds a message to an error.

func WithMessage(err error, message string) error
Wrap

Wraps an error with a message.

func Wrap(err error, message string) error
GetContext

Gets context information from an error.

func GetContext(err error, key string) (interface{}, bool)
GetOp

Gets operation information from an error.

func GetOp(err error) (string, bool)
GetMessage

Gets the message from an error.

func GetMessage(err error) (string, bool)

Examples

For complete, runnable examples, see the following directories in the EXAMPLES directory:

Best Practices

  1. Use Specific Error Types: Use the most specific error type for the situation
  2. Add Context Information: Always add relevant context information to errors
  3. Track Operations: Always track the operation that caused an error
  4. Check Error Types: Check error types before handling them
  5. Log Errors: Always log errors with their context information

Troubleshooting

Common Issues
Error Type Checking

If error type checking fails, ensure you're using the Is method or errors.Is function.

  • Auth - The parent auth package
  • Errors - The core errors package
  • Logging - Logging for errors

Contributing

Contributions to this component are welcome! Please see the Contributing Guide for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package errors provides comprehensive error handling for the auth module. It defines error types and provides context-aware error handling.

IMPORTANT: This package is now implemented using the main errors framework while maintaining backward compatibility with existing code. New code should use the main errors framework directly instead of this package.

The main errors framework provides a more comprehensive error handling system with better integration with the hybrid architecture. It includes error types for different layers (domain, application, infrastructure) and better support for error context, HTTP status mapping, and error recovery.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is returned when a token is invalid
	ErrInvalidToken = errors.NewAuthenticationError("invalid token", "", nil)

	// ErrExpiredToken is returned when a token has expired
	ErrExpiredToken = errors.NewAuthenticationError("token expired", "", nil)

	// ErrMissingToken is returned when a token is missing
	ErrMissingToken = errors.NewAuthenticationError("token missing", "", nil)

	// ErrInvalidSignature is returned when a token has an invalid signature
	ErrInvalidSignature = errors.NewAuthenticationError("invalid token signature", "", nil)

	// ErrInvalidClaims is returned when a token has invalid claims
	ErrInvalidClaims = errors.NewAuthenticationError("invalid token claims", "", nil)

	// ErrUnauthorized is returned when a user is not authorized to perform an operation
	ErrUnauthorized = errors.ErrUnauthorized

	// ErrForbidden is returned when a user is forbidden from performing an operation
	ErrForbidden = errors.ErrForbidden

	// ErrInvalidConfig is returned when the configuration is invalid
	ErrInvalidConfig = errors.NewConfigurationError("invalid configuration", "", "", nil)

	// ErrInternal is returned when an internal error occurs
	ErrInternal = errors.ErrInternal

	// ErrNotImplemented is returned when a feature is not implemented
	ErrNotImplemented = errors.New(errors.InternalErrorCode, "not implemented")
)

Standard error types

Functions

func GetContext

func GetContext(err error, key string) (interface{}, bool)

GetContext gets a context value from an error

func GetMessage

func GetMessage(err error) (string, bool)

GetMessage gets the message from an error

func GetOp

func GetOp(err error) (string, bool)

GetOp gets the operation from an error

func WithContext

func WithContext(err error, key string, value interface{}) error

WithContext adds context to an error

func WithMessage

func WithMessage(err error, message string) error

WithMessage adds a message to an error

func WithOp

func WithOp(err error, op string) error

WithOp adds an operation to an error

func Wrap

func Wrap(err error, message string) error

Wrap wraps an error with a message

Types

type AuthError

type AuthError struct {
	// Err is the underlying error
	Err error

	// Op is the operation that caused the error
	Op string

	// Message is a human-readable message
	Message string

	// Context contains additional context for the error
	Context map[string]interface{}
}

AuthError represents an authentication or authorization error with context It's now a wrapper around the new errors framework's AuthenticationError

func NewAuthError

func NewAuthError(err error, op string, message string, context map[string]interface{}) *AuthError

NewAuthError creates a new AuthError

func (*AuthError) Error

func (e *AuthError) Error() string

Error implements the error interface

func (*AuthError) Is

func (e *AuthError) Is(target error) bool

Is reports whether the error is of the target type

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

Unwrap returns the underlying error

Jump to

Keyboard shortcuts

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