-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patherror.go
More file actions
35 lines (30 loc) · 794 Bytes
/
error.go
File metadata and controls
35 lines (30 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ginmiddleware
import (
"errors"
"net/http"
"github.com/getkin/kin-openapi/routers"
"github.com/gin-gonic/gin"
)
func handleValidationError(
c *gin.Context,
err error,
options *Options,
generalStatusCode int,
) {
var errorHandler ErrorHandler
// if an error handler is provided, use that
if options != nil && options.ErrorHandler != nil {
errorHandler = options.ErrorHandler
} else {
errorHandler = func(c *gin.Context, message string, statusCode int) {
c.AbortWithStatusJSON(statusCode, gin.H{"error": message})
}
}
if errors.Is(err, routers.ErrPathNotFound) {
errorHandler(c, err.Error(), http.StatusNotFound)
} else {
errorHandler(c, err.Error(), generalStatusCode)
}
// in case the handler didn't internally call Abort, stop the chain
c.Abort()
}