-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
32 lines (26 loc) · 880 Bytes
/
errors.go
File metadata and controls
32 lines (26 loc) · 880 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
package openapi_validator
import (
"encoding/json"
"net/http"
)
// ValidationError represents a failed validation attempt.
// It includes a top-level message and an optional list of detailed error strings.
type ValidationError struct {
Message string `json:"message"`
Errors []string `json:"errors,omitempty"`
}
// Error implements the error interface for ValidationError.
func (e *ValidationError) Error() string {
return e.Message
}
// DefaultErrorEncoder is a built-in implementation of ErrorEncoder.
// It sends a JSON response with a 400 Bad Request status code.
func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
resp := ValidationError{
Message: "Validation Failed",
Errors: []string{err.Error()},
}
json.NewEncoder(w).Encode(resp)
}