Categorygithub.com/cyverse-de/go-mod/httperror
modulepackage
0.0.1
Repository: https://github.com/cyverse-de/go-mod.git
Documentation: pkg.go.dev

# README

httperror

Handles some of the boilerplate logic needed for error handling with Labstack Echo so that Echo errors can be treated like normal Go errors and will also be marshalled into a standardized JSON format.

Examples

Setting up the custom error handler in Echo:

package main

import (
    "github.com/labstack/echo/v4"
    "github.com/cyverse-de/go-mod/httperror"
)

type App struct{
    router *echo.Echo
}

func New() *App {
    r := echo.New()

    // This is the important bit.
    r.HTTPErrorHandler = httperror.HTTPErrorHandler

    return &App{
        router: r,
    }
}

...

Raising an error from an Echo HTTP handler:

func (a *App) ContrivedExampleHandler(c echo.Context) error {
    c := c.Param("id")
    if id == "" {
        // With the above error handler registered, echo Errors can be returned
        // like normal errors and they get marshalled into a consistent JSON
        // format.
        return echo.NewHTTPError(http.StatusBadRequest, errors.New("id was not set"))
    }
    ...do stuff...
}

# Functions

DetailedError responds to an HTTP request with a JSON response body indicating that an error occurred and providing some extra details about the error if additional details are available.
Error responds to an HTTP request with a JSON response body indicating that an error occurred.
HTTPErrorHandler is an echo.HTTPErrorHandler for this application.
NewErrorResponse constructs an ErrorResponse based on the message passed in, but does not send it over the wire.

# Variables

No description provided by the author

# Structs

ErrorResponse represents an HTTP response body containing error information.