apierror

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: Apache-2.0 Imports: 1 Imported by: 7

Documentation

Overview

Package apierror provides structured error handling for HTTP APIs.

This package defines a standardized error interface for creating consistent, JSON-serializable API errors with metadata support. All errors implement custom JSON marshaling for consistent API responses and support fluent composition for building complex error responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError interface {
	Error() string
	ID() string
	Data() any
	Message() string
	Origin() string
}

APIError represents a custom error type.

type DefaultAPIError

type DefaultAPIError struct {
	ErrID      string `json:"id"`
	ErrData    any    `json:"data,omitempty"`
	ErrMessage string `json:"message,omitempty"`
	ErrOrigin  string `json:"origin,omitempty"`
}

DefaultAPIError represents a JSON marshalable custom error type.

func APIErrorFrom

func APIErrorFrom(err APIError) *DefaultAPIError

APIErrorFrom converts an APIError to a DefaultAPIError.

Parameters:

  • err: The APIError to convert.

Returns:

  • *DefaultAPIError: A new DefaultAPIError instance.

func NewAPIError

func NewAPIError(id string) *DefaultAPIError

NewAPIError returns a new error with the given ID.

Parameters:

  • id: The ID of the error.

Returns:

  • *DefaultAPIError: A new DefaultAPIError instance.

func (*DefaultAPIError) Data

func (e *DefaultAPIError) Data() any

Data returns the data associated with the error.

Returns:

  • any: The data associated with the error.

func (*DefaultAPIError) Error

func (e *DefaultAPIError) Error() string

Error returns the full error message as a string. If the error has a message, it returns the ID followed by the message. Otherwise, it returns just the ID.

Returns:

  • string: The full error message as a string.

func (*DefaultAPIError) ID

func (e *DefaultAPIError) ID() string

ID returns the ID of the error.

Returns:

  • string: The ID of the error.

func (*DefaultAPIError) Message

func (e *DefaultAPIError) Message() string

Message returns the message associated with the error.

Returns:

  • string: The message associated with the error.

func (*DefaultAPIError) Origin

func (e *DefaultAPIError) Origin() string

Origin returns the origin associated with the error.

Returns:

  • string: The origin associated with the error.

func (*DefaultAPIError) WithData

func (e *DefaultAPIError) WithData(data any) *DefaultAPIError

WithData returns a new error with the given data.

Parameters:

  • data: The data to include in the error.

Returns:

  • *DefaultAPIError: A new DefaultAPIError.

func (*DefaultAPIError) WithID

func (e *DefaultAPIError) WithID(id string) *DefaultAPIError

WithID returns a new error with the given ID.

Parameters:

  • id: The ID to include in the error.

Returns:

  • *DefaultAPIError: A new DefaultAPIError.

func (*DefaultAPIError) WithMessage

func (e *DefaultAPIError) WithMessage(message string) *DefaultAPIError

WithMessage returns a new error with the given message.

Parameters:

  • message: The message to include in the error.

Returns:

  • *DefaultAPIError: A new DefaultAPIError.

func (*DefaultAPIError) WithOrigin

func (e *DefaultAPIError) WithOrigin(origin string) *DefaultAPIError

WithOrigin returns a new error with the given origin.

Parameters:

  • origin: The origin to include in the error.

Returns:

  • *DefaultAPIError: A new DefaultAPIError.

Jump to

Keyboard shortcuts

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