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-02-07 08:59:34 -06:00
.gitignore 🙈 Add .gitignore 2024-01-17 15:47:57 -06:00
CHANGELOG.md 📝 Update Changelog 2024-02-07 08:59:08 -06:00
errorf.go Add JSON marshalling 2024-01-21 13:50:13 -06:00
errorf_test.go Add JSON marshalling 2024-01-21 13:50:13 -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 JSON marshalling 2024-01-21 13:50:13 -06:00
LICENSE 📄 Switch to AGPL 2024-01-17 19:54:15 -06:00
new.go Add 401 and 403 default errors 2024-02-07 08:57:14 -06:00
new_test.go Add 401 and 403 default errors 2024-02-07 08:57:14 -06:00
README.md 📝 Update Changelog and README 2024-01-21 13:56:19 -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

Any suggestions/requests?