Add err returns

This commit is contained in:
Melody Becker 2025-04-24 15:58:06 +02:00
parent 4013622afc
commit 2b737d056c
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI

View file

@ -12,10 +12,11 @@ import (
// and a field "message" set to the `message` // and a field "message" set to the `message`
// //
// Deprecated: Use ProblemDetails or ProblemDetailsStatusOnly instead // Deprecated: Use ProblemDetails or ProblemDetailsStatusOnly instead
func HttpErr(w http.ResponseWriter, errId int, message string, code int) { func HttpErr(w http.ResponseWriter, errId int, message string, code int) error {
w.WriteHeader(code) w.WriteHeader(code)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, "{\"id\": %d, \"message\": \"%s\"}", errId, message) _, err := fmt.Fprintf(w, "{\"id\": %d, \"message\": \"%s\"}", errId, message)
return err
} }
// Write an RFC 9457 compliant problem details response // Write an RFC 9457 compliant problem details response
@ -31,7 +32,7 @@ func ProblemDetails(
errorTitle string, errorTitle string,
details *string, details *string,
extras map[string]any, extras map[string]any,
) { ) error {
w.Header().Add("Content-Type", "application/problem+json") w.Header().Add("Content-Type", "application/problem+json")
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
data := map[string]any{ data := map[string]any{
@ -42,7 +43,6 @@ func ProblemDetails(
if details != nil { if details != nil {
data["detail"] = *details data["detail"] = *details
} }
if extras != nil {
for k, v := range extras { for k, v := range extras {
if _, ok := data[k]; ok { if _, ok := data[k]; ok {
// Don't overwrite default fields // Don't overwrite default fields
@ -50,14 +50,13 @@ func ProblemDetails(
} }
data[k] = v data[k] = v
} }
}
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
enc.Encode(data) return enc.Encode(data)
} }
// Write a simple problem details response. // Write a simple problem details response.
// It only provides the status code, as defined in RFC 9457, section 4.2.1 // It only provides the status code, as defined in RFC 9457, section 4.2.1
func ProblemDetailsStatusOnly(w http.ResponseWriter, statusCode int) { func ProblemDetailsStatusOnly(w http.ResponseWriter, statusCode int) error {
w.Header().Add("Content-Type", "application/problem+json") w.Header().Add("Content-Type", "application/problem+json")
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
data := map[string]any{ data := map[string]any{
@ -68,5 +67,5 @@ func ProblemDetailsStatusOnly(w http.ResponseWriter, statusCode int) {
} }
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
enc.Encode(data) return enc.Encode(data)
} }