35 lines
660 B
Go
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
|
|
}
|