gin error handler
This commit is contained in:
@@ -212,7 +212,7 @@ func (a *Auth) AuthenticateAndAddIdentityToRequestGin(assertions ...Authorizatio
|
||||
if errors.Is(err, consts.ErrNotFound) {
|
||||
u, err := a.newLoginURL(ctx, a.auth, r.URL.String())
|
||||
if err != nil {
|
||||
response.WriteError(c, response.Errorf("failed to generate login url: %w", err))
|
||||
c.Error(response.Errorf("failed to generate login url: %w", err))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -222,7 +222,7 @@ func (a *Auth) AuthenticateAndAddIdentityToRequestGin(assertions ...Authorizatio
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteError(c, response.Errorf("failed to authenticate: %w", err))
|
||||
c.Error(response.Errorf("failed to authenticate: %w", err))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -255,7 +255,7 @@ func (a *Auth) AuthenticateAndAddIdentityToRequestGin(assertions ...Authorizatio
|
||||
|
||||
user, acct, err := a.accts.GetUserAndAccountByAccessToken(ctx, accessToken)
|
||||
if err != nil {
|
||||
response.WriteError(c, response.Errorf("failed to authorize: %w", err))
|
||||
c.Error(response.Errorf("failed to authorize: %w", err))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -263,7 +263,7 @@ func (a *Auth) AuthenticateAndAddIdentityToRequestGin(assertions ...Authorizatio
|
||||
for _, as := range assertions {
|
||||
res, err := as(c)
|
||||
if err != nil {
|
||||
response.WriteError(c, err)
|
||||
c.Error(err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// TODO: make this GIN compatible
|
||||
func LogRequests(ctx context.Context, logger *logging.Logger) response.Middleware {
|
||||
reqIDCh := newRequestIDProvider(ctx)
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ func Handler(f HandlerFunc) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
res, err := f(c)
|
||||
if err != nil {
|
||||
WriteError(c, err)
|
||||
c.Error(err)
|
||||
} else {
|
||||
Write(c, res)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,44 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HandleErrors(c *gin.Context) {
|
||||
c.Next()
|
||||
|
||||
if len(c.Errors) == 0 || c.Writer.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
err ErrorResponse
|
||||
ok bool
|
||||
)
|
||||
for _, e := range c.Errors {
|
||||
if err, ok = GetError(e); ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
status, ok := err.GetStatus()
|
||||
if !ok {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
if h, ok := err.GetHTML(); ok {
|
||||
c.Status(status)
|
||||
c.Header("Content-Type", "text/html")
|
||||
c.Writer.Write(h)
|
||||
return
|
||||
}
|
||||
|
||||
msg, ok := err.GetMsg()
|
||||
if !ok {
|
||||
msg = err.Error()
|
||||
}
|
||||
c.String(status, msg)
|
||||
}
|
||||
|
||||
func Write(c *gin.Context, res Response) {
|
||||
w := c.Writer
|
||||
|
||||
@@ -51,17 +89,6 @@ func Write(c *gin.Context, res Response) {
|
||||
|
||||
}
|
||||
|
||||
func WriteError(c *gin.Context, err error) {
|
||||
status := GetStatusFromError(err)
|
||||
if h, ok := GetHTMLFromError(err); ok {
|
||||
c.Status(status)
|
||||
c.Header("Content-Type", "text/html")
|
||||
c.Writer.Write(h)
|
||||
} else {
|
||||
c.String(status, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func GetStatusFromError(err error) int {
|
||||
status := http.StatusInternalServerError
|
||||
|
||||
@@ -73,12 +100,3 @@ func GetStatusFromError(err error) int {
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
func GetHTMLFromError(err error) ([]byte, bool) {
|
||||
e, ok := GetError(err)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return e.GetHTML()
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"ruben/inventory2/internal/server/api/webhooks"
|
||||
etsy_webhooks "ruben/inventory2/internal/server/api/webhooks/etsy"
|
||||
"ruben/inventory2/internal/server/middleware"
|
||||
"ruben/inventory2/internal/server/response"
|
||||
"ruben/inventory2/internal/server/sse"
|
||||
)
|
||||
|
||||
@@ -40,6 +41,8 @@ func NewRouter(
|
||||
) *Router {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(response.HandleErrors)
|
||||
|
||||
// TODO: shouldn't this ACTUALLY be a middleware?
|
||||
// - only try to make this an actual middleware AFTER all the routers are broken out, so that way how the middleware is supposed to work can be known
|
||||
authMiddleware := middleware.NewAuth(
|
||||
|
||||
Reference in New Issue
Block a user