Small go library that adds errors which are better for use in a response. https://pkg.go.dev/codeberg.org/danjones000/responsable-errors
Find a file
2024-01-17 19:54:15 -06:00
.gitignore 🙈 Add .gitignore 2024-01-17 15:47:57 -06:00
CHANGELOG.md 📝 Create CHANGELOG 2024-01-17 16:19:10 -06:00
errorf.go Add SettableError 2024-01-17 15:51:28 -06:00
errorf_test.go Add SettableError 2024-01-17 15:51:28 -06:00
go.mod Add errors.Errorf 2024-01-17 10:58:39 -06:00
go.sum Add errors.Errorf 2024-01-17 10:58:39 -06:00
interface.go Add SettableError 2024-01-17 15:51:28 -06:00
LICENSE 📄 Switch to AGPL 2024-01-17 19:54:15 -06:00
new.go Add a few common responses 2024-01-17 15:52:08 -06:00
new_test.go Add a few common responses 2024-01-17 15:52:08 -06:00
README.md 📝 Add LICENSE and README 2024-01-17 16:11:02 -06:00

Responsable Errors

This is a small go library that provides errors that can be more useful in the context of a web app.

Overview

Use this library to wrap errors so that an error handler in your web app can use them in a response.

I plan to create a middleware library for Gin soon to utilize this library.

Installation

Use go modules.

import errors "codeberg.org/danjones000/responsable-errors"

Usage

err := SomethingThatReturnsAnError()
err = errors.Errorf(418, "I am a teapot. %w", err)

// Later on, when you're returning something
// This example uses gin.
c.JSON(err.GetStatus(), gin.H{"error":err.GetMsg()})

Or:

user, err := db.GetUser(id)
if err != nil {
    return errors.NotFound("%w", err).Msg("User %d not found", id)
}

In the second example, err.GetStatus() returns 404, err.GetMsg() returns "User XX not found", and err.Error() returns the error message from the original error. Also, err.Unwrap() will return the original error, but you'll have to type cast to UnwrappableError first.

Future plans

I'm thinking about making the errors able to return a default body from json.Marshal, possibly with an optional override.`