Files
lndhub.go/lib/responses/errors.go
Michael Bumann dd0d168b5b Use the HTTP error code define by the error if available
This handler handles all errors. those are not necessarily http 500 erros.
We should use the code from the error if available
2022-01-27 18:21:36 +01:00

61 lines
1.3 KiB
Go

package responses
import (
"net/http"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
)
type ErrorResponse struct {
Error bool `json:"error"`
Code int `json:"code"`
Message string `json:"message"`
}
var GeneralServerError = ErrorResponse{
Error: true,
Code: 6,
Message: "Something went wrong. Please try again later",
}
var BadArgumentsError = ErrorResponse{
Error: true,
Code: 8,
Message: "Bad arguments",
}
var BadAuthError = ErrorResponse{
Error: true,
Code: 1,
Message: "bad auth",
}
var NotEnoughBalanceError = ErrorResponse{
Error: true,
Code: 2,
Message: "not enough balance. Make sure you have at least 1%% reserved for potential fees",
}
func HTTPErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
return
}
c.Logger().Error(err)
if hub := sentryecho.GetHubFromContext(c); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("UserID", c.Get("UserID"))
hub.CaptureException(err)
})
}
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
c.JSON(code, he.Message)
} else {
c.JSON(http.StatusInternalServerError, GeneralServerError)
}
// TODO: use an error matching the error code
}