(w http.ResponseWriter, req *http.Request, m proto.Message, span oteltrace.Span)
| 403 | } |
| 404 | |
| 405 | func writeFormattedContentForRequest(w http.ResponseWriter, req *http.Request, m proto.Message, span oteltrace.Span) { |
| 406 | // Check for both explicit nil and typed nil pointers (e.g., (*tempopb.SearchResponse)(nil)) |
| 407 | // A typed nil pointer is not equal to nil when passed as an interface, so we need reflection |
| 408 | if m == nil || (reflect.ValueOf(m).Kind() == reflect.Ptr && reflect.ValueOf(m).IsNil()) { |
| 409 | http.Error(w, "internal error: nil response", http.StatusInternalServerError) |
| 410 | if span != nil { |
| 411 | span.RecordError(fmt.Errorf("nil response in writeFormattedContentForRequest")) |
| 412 | } |
| 413 | return |
| 414 | } |
| 415 | |
| 416 | switch req.Header.Get(api.HeaderAccept) { |
| 417 | case api.HeaderAcceptProtobuf: |
| 418 | b, err := proto.Marshal(m) |
| 419 | if err != nil { |
| 420 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 421 | return |
| 422 | } |
| 423 | |
| 424 | w.Header().Set(api.HeaderContentType, api.HeaderAcceptProtobuf) |
| 425 | _, err = w.Write(b) |
| 426 | if err != nil { |
| 427 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 428 | return |
| 429 | } |
| 430 | if span != nil { |
| 431 | span.SetAttributes(attribute.String("contentType", api.HeaderAcceptProtobuf)) |
| 432 | } |
| 433 | |
| 434 | default: |
| 435 | w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON) |
| 436 | err := new(jsonpb.Marshaler).Marshal(w, m) |
| 437 | if err != nil { |
| 438 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 439 | return |
| 440 | } |
| 441 | if span != nil { |
| 442 | span.SetAttributes(attribute.String("contentType", api.HeaderAcceptJSON)) |
| 443 | } |
| 444 | |
| 445 | } |
| 446 | } |
no test coverage detected