apierror

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package apierror provides typed HTTP errors and a Fiber ErrorHandler that renders them (and *fiber.Error, and unknown errors) as the standard JSON envelope. Handlers and services return an *Error; bootstrap wires Handler by default so the response is consistent.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(c fiber.Ctx, err error) error

Handler is a fiber.ErrorHandler that renders errors as the standard JSON envelope: *Error at its status with its code, *fiber.Error at its code, and any other error as a generic 500 (the raw message is not exposed).

Example

Return a typed error from a handler; the ErrorHandler renders it as JSON.

package main

import (
	"fmt"
	"io"
	"net/http/httptest"

	"github.com/gofiber/fiber/v3"
	"github.com/rahmadafandi/fibr/v2/apierror"
)

func main() {
	app := fiber.New(fiber.Config{ErrorHandler: apierror.Handler})
	app.Get("/users/:id", func(c fiber.Ctx) error {
		return apierror.NotFound("user not found").WithCode("user_not_found")
	})

	resp, _ := app.Test(httptest.NewRequest("GET", "/users/9", nil))
	body, _ := io.ReadAll(resp.Body)

	fmt.Println(resp.StatusCode)
	fmt.Println(string(body))
}
Output:
404
{"code":404,"message":"user not found","error":"user_not_found","status":"error"}

func StatusOf

func StatusOf(err error) int

StatusOf returns the HTTP status Handler would respond with for err: an *Error's Status, a *fiber.Error's Code, 500 for any other error, and 200 for nil. Middleware uses it to learn the final status before Fiber's error handler has run.

Types

type Error

type Error struct {
	Status  int
	Code    string
	Message string
	Details any
	// contains filtered or unexported fields
}

Error is a typed HTTP error: an HTTP status, a machine-readable code, a human message, optional details (rendered into "data"), and an optional wrapped cause (for errors.Is/As and logging; never serialized).

func BadRequest

func BadRequest(message string) *Error

BadRequest returns a 400 Error.

func Conflict

func Conflict(message string) *Error

Conflict returns a 409 Error.

func Forbidden

func Forbidden(message string) *Error

Forbidden returns a 403 Error.

func Internal

func Internal(message string) *Error

Internal returns a 500 Error.

func New

func New(status int, code, message string) *Error

New builds an Error with an explicit status and code.

func NotFound

func NotFound(message string) *Error

NotFound returns a 404 Error.

func TooManyRequests

func TooManyRequests(message string) *Error

TooManyRequests returns a 429 Error.

func Unauthorized

func Unauthorized(message string) *Error

Unauthorized returns a 401 Error.

func UnprocessableEntity

func UnprocessableEntity(message string) *Error

UnprocessableEntity returns a 422 Error.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped cause, if any.

func (*Error) WithCode

func (e *Error) WithCode(code string) *Error

WithCode overrides the default machine code (e.g. "email_taken"). It returns a copy, so it is safe to call on shared/sentinel values.

func (*Error) WithDetails

func (e *Error) WithDetails(d any) *Error

WithDetails attaches details rendered into the response "data" field. It returns a copy, so it is safe to call on shared/sentinel values.

func (*Error) Wrap

func (e *Error) Wrap(err error) *Error

Wrap attaches an underlying cause (never exposed in the response). It returns a copy, so it is safe to call on shared/sentinel values.

Jump to

Keyboard shortcuts

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