handlers

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 7 Imported by: 0

README

handlers

Common HTTP handler utilities for Gin.

Usage

import "github.com/adeptry-app/go-common/handlers"

// Error responses
handlers.RespondError(c, http.StatusBadRequest, "Invalid input")
handlers.LogAndRespondError(c, http.StatusInternalServerError, err, "Operation failed")
handlers.HandleRepositoryError(c, err, "Resource not found", "Database error")

// Location header for created resources
handlers.SetLocationHeader(c, resourceID) // Sets Location: /current/path/{id}

Documentation

Overview

Package handlers provides common HTTP handler utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleRepositoryError

func HandleRepositoryError(c *gin.Context, err error, notFoundMsg, internalMsg string)

HandleRepositoryError checks if the error is a record not found error and responds with appropriate HTTP status code (404 or 500). For internal errors, it logs the error with structured logging.

func LogAndRespondError

func LogAndRespondError(c *gin.Context, statusCode int, err error, userMsg string)

LogAndRespondError logs the error with context and responds with the given status code. Use this for non-repository errors (auth failures, validation errors, external service errors).

func RespondError

func RespondError(c *gin.Context, statusCode int, userMsg string)

RespondError responds with an error without logging (for expected errors like validation failures). Use this when the error is not exceptional and doesn't need logging (e.g., invalid input). Sole producer of the error envelope every service returns.

func SetLocationHeader

func SetLocationHeader(c *gin.Context, id int64)

SetLocationHeader sets the Location header for created resources. Follows REST convention of providing URI to newly created resource.

Types

This section is empty.

Jump to

Keyboard shortcuts

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