This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
summer-utils/api/crud/http-errors.go
2023-04-16 11:56:17 +01:00

35 lines
660 B
Go

package crud
import (
"code.mrmelon54.com/melon/summer/pkg/api"
"fmt"
"github.com/pkg/errors"
"net/http"
)
var (
ErrForbidden = HttpError(http.StatusForbidden)
ErrMethodNotAllowed = HttpError(http.StatusMethodNotAllowed)
)
type HttpError int
func (h HttpError) Error() string {
return fmt.Sprintf("%d %s", h, http.StatusText(int(h)))
}
var catchErrors = []HttpError{
ErrForbidden,
ErrMethodNotAllowed,
}
func HttpCatchError(rw http.ResponseWriter, err error) bool {
for _, i := range catchErrors {
if errors.Is(err, i) {
api.GenericErrorMsg[HttpError](rw, err, http.StatusForbidden, i.Error())
return true
}
}
return false
}