-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
27 lines (23 loc) · 697 Bytes
/
utils.go
File metadata and controls
27 lines (23 loc) · 697 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
package main
import (
"encoding/json"
"net/http"
)
// JSONResponse is a helper function to send JSON responses
func JSONResponse(w http.ResponseWriter, data any, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(data)
}
// ErrorResponse represents a standardized error response
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message,omitempty"`
}
// JSONErrorResponse sends a standardized JSON error response
func JSONErrorResponse(w http.ResponseWriter, message string, status int) {
JSONResponse(w, ErrorResponse{
Error: http.StatusText(status),
Message: message,
}, status)
}