Add err returns
This commit is contained in:
parent
4013622afc
commit
2b737d056c
1 changed files with 12 additions and 13 deletions
|
@ -12,10 +12,11 @@ import (
|
|||
// and a field "message" set to the `message`
|
||||
//
|
||||
// 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.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
|
||||
|
@ -31,7 +32,7 @@ func ProblemDetails(
|
|||
errorTitle string,
|
||||
details *string,
|
||||
extras map[string]any,
|
||||
) {
|
||||
) error {
|
||||
w.Header().Add("Content-Type", "application/problem+json")
|
||||
w.WriteHeader(statusCode)
|
||||
data := map[string]any{
|
||||
|
@ -42,22 +43,20 @@ func ProblemDetails(
|
|||
if details != nil {
|
||||
data["detail"] = *details
|
||||
}
|
||||
if extras != nil {
|
||||
for k, v := range extras {
|
||||
if _, ok := data[k]; ok {
|
||||
// Don't overwrite default fields
|
||||
continue
|
||||
}
|
||||
data[k] = v
|
||||
for k, v := range extras {
|
||||
if _, ok := data[k]; ok {
|
||||
// Don't overwrite default fields
|
||||
continue
|
||||
}
|
||||
data[k] = v
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
enc.Encode(data)
|
||||
return enc.Encode(data)
|
||||
}
|
||||
|
||||
// Write a simple problem details response.
|
||||
// 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.WriteHeader(statusCode)
|
||||
data := map[string]any{
|
||||
|
@ -68,5 +67,5 @@ func ProblemDetailsStatusOnly(w http.ResponseWriter, statusCode int) {
|
|||
}
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.Encode(data)
|
||||
return enc.Encode(data)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue