(code int, msg string, rw http.ResponseWriter, r *http.Request)
| 64 | } |
| 65 | |
| 66 | func ReturnError(code int, msg string, rw http.ResponseWriter, r *http.Request) { |
| 67 | query := r.URL.Query() |
| 68 | isJson := query.Get("format") == "json" |
| 69 | if isJson { |
| 70 | if err := json.NewEncoder(rw).Encode(APIError{code, msg}); err != nil { |
| 71 | json.NewEncoder(rw).Encode(APIError{ |
| 72 | Code: APIErrorJSONEncode, |
| 73 | Message: err.Error(), |
| 74 | }) |
| 75 | } |
| 76 | } else { |
| 77 | switch true { |
| 78 | case code == 0: |
| 79 | http.Error(rw, msg, http.StatusOK) |
| 80 | case code/10 == 404: |
| 81 | http.Error(rw, msg, http.StatusNotFound) |
| 82 | case code > 5000: |
| 83 | http.Error(rw, msg, http.StatusInternalServerError) |
| 84 | default: |
| 85 | http.Error(rw, msg, http.StatusBadRequest) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | func ReturnFetchList[T any](fetch func() []T, rw http.ResponseWriter, r *http.Request) { |
| 90 | query := r.URL.Query() |
| 91 | isYaml := query.Get("format") == "yaml" |
no test coverage detected