-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors.go
More file actions
37 lines (32 loc) · 988 Bytes
/
errors.go
File metadata and controls
37 lines (32 loc) · 988 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
33
34
35
36
37
package api
import (
"encoding/json"
"errors"
"net/http"
"github.com/golang/glog"
"github.com/livepeer/livepeer-data/health"
"github.com/livepeer/livepeer-data/views"
)
type errorResponse struct {
Errors []string `json:"errors"`
}
func respondError(rw http.ResponseWriter, defaultStatus int, errs ...error) {
status := defaultStatus
response := errorResponse{}
for _, err := range errs {
response.Errors = append(response.Errors, err.Error())
if errors.Is(err, health.ErrStreamNotFound) ||
errors.Is(err, health.ErrEventNotFound) ||
errors.Is(err, views.ErrAssetNotFound) {
status = http.StatusNotFound
}
}
respondJson(rw, status, response)
}
func respondJson(rw http.ResponseWriter, status int, response interface{}) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(status)
if err := json.NewEncoder(rw).Encode(response); err != nil {
glog.Errorf("Error writing response. err=%q, response=%+v", err, response)
}
}