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.
56 lines
988 B
56 lines
988 B
3 years ago
|
package routes
|
||
3 years ago
|
|
||
|
import (
|
||
3 years ago
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
3 years ago
|
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||
3 years ago
|
"github.com/sirupsen/logrus"
|
||
3 years ago
|
|
||
|
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
|
||
3 years ago
|
)
|
||
|
|
||
|
// errorNotFound renders http error-404 template
|
||
3 years ago
|
func (r *Routes) errorNotFound(c *routing.Context) (err error) {
|
||
3 years ago
|
|
||
|
errorContent := content.Error{Message: "not found"}
|
||
|
|
||
|
return c.Write(errorContent)
|
||
|
}
|
||
3 years ago
|
|
||
|
// useErrorHandler is the middleware that catch handlers errors and render error template
|
||
3 years ago
|
func (r *Routes) useErrorHandler(c *routing.Context) (err error) {
|
||
3 years ago
|
|
||
|
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)
|
||
|
}
|