You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nikita-tokarch-uk/pkg/routes/error.go

56 lines
988 B

package routes
import (
"fmt"
"net/http"
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/sirupsen/logrus"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
)
// errorNotFound renders http error-404 template
func (r *Routes) errorNotFound(c *routing.Context) (err error) {
errorContent := content.Error{Message: "not found"}
return c.Write(errorContent)
}
// useErrorHandler is the middleware that catch handlers errors and render error template
func (r *Routes) useErrorHandler(c *routing.Context) (err error) {
worker := func() (err error) {
defer func() {
r := recover()
if r == nil {
return
}
err = routing.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("panic:\n%v", r))
}()
err = c.Next()
return
}
err = worker()
if err == nil {
return
}
c.Abort()
logrus.Warnf("Cannot process request, %v", err)
errorContent := content.Error{
Message: err.Error(),
}
return c.Write(errorContent)
}