handleJSON manages decoding the request's body JSON as data and passing it on to the provided handler function.
(h func(ctx context.Context, data data) error)
| 532 | // handleJSON manages decoding the request's body JSON as data and passing it |
| 533 | // on to the provided handler function. |
| 534 | func handleJSON[data any](h func(ctx context.Context, data data) error) http.HandlerFunc { |
| 535 | return func(w http.ResponseWriter, r *http.Request) { |
| 536 | defer r.Body.Close() |
| 537 | var body data |
| 538 | if err := json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 539 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 540 | return |
| 541 | } |
| 542 | if err := h(r.Context(), body); err != nil { |
| 543 | if httpErr, ok := errors.AsType[tsweb.HTTPError](err); ok { |
| 544 | tsweb.WriteHTTPError(w, r, httpErr) |
| 545 | } else { |
| 546 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 547 | } |
| 548 | return |
| 549 | } |
| 550 | w.WriteHeader(http.StatusOK) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | var contextKeyPeer = ctxkey.New("peer-capabilities", peerCapabilities{}) |
| 555 |
nothing calls this directly
no test coverage detected
searching dependent graphs…