errors.go

Overview

The errors.go file defines a specialized error type used within the controllers package to encapsulate an error along with an associated HTTP status code. This facilitates error handling in HTTP request processing by bundling error details and corresponding status codes together. The main functionality revolves around the statusError struct and its methods, which provide a convenient way to propagate error messages alongside HTTP status codes through the system.

This file is typically used in the context of HTTP controllers or REST API handlers where errors need to return both an error message and an HTTP status to clients. Its simple design supports integration with error handling middleware or response writers that inspect the status code to determine the appropriate HTTP response.

Types and Functions

statusError

A struct that wraps an error with an HTTP status code.

type statusError struct {
    Err  error
    Code int
}

This struct is unexported (lowercase name), indicating it is intended for internal use within the controllers package.

newStatusError

Constructor function that creates a new statusError.

func newStatusError(err error, code int) statusError
se := newStatusError(fmt.Errorf("resource not found"), 404)

This encapsulates an error with a 404 Not Found HTTP status.

(statusError) Error

Implements the standard error interface for statusError by forwarding the call to the wrapped error's Error() method.

func (se statusError) Error() string

(statusError) Status

Accessor method returning the HTTP status code associated with the error.

func (se statusError) Status() int

Implementation Details

Interaction with Other System Components

Mermaid Diagram

classDiagram
class statusError {
-Err: error
-Code: int
+Error() string
+Status() int
}
class newStatusError {
+newStatusError(err, code) statusError
}
newStatusError --> statusError : creates

This diagram illustrates the statusError struct with its fields and methods, and the newStatusError factory function that creates instances of statusError.