-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhttpresponse.go
More file actions
32 lines (28 loc) · 690 Bytes
/
httpresponse.go
File metadata and controls
32 lines (28 loc) · 690 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 kilonova
import (
"context"
"encoding/json"
"errors"
"log/slog"
"net/http"
)
func StatusData(w http.ResponseWriter, status string, retData any, statusCode int) {
if err, ok := retData.(error); ok {
retData = err.Error()
if sErr, ok2 := errors.AsType[*statusError](err); ok2 {
statusCode = sErr.Code
}
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(statusCode)
err := json.NewEncoder(w).Encode(struct {
Status string `json:"status"`
Data any `json:"data"`
}{
Status: status,
Data: retData,
})
if err != nil {
slog.ErrorContext(context.Background(), "Couldn't send return data", slog.Any("err", err))
}
}