(api API, ctx Context, status int, ct string, body any)
| 557 | } |
| 558 | |
| 559 | func writeResponse(api API, ctx Context, status int, ct string, body any) error { |
| 560 | if ct == "" { |
| 561 | // If no content type was provided, try to negotiate one with the client. |
| 562 | var err error |
| 563 | ct, err = api.Negotiate(ctx.Header("Accept")) |
| 564 | if err != nil { |
| 565 | notAccept := NewErrorWithContext(ctx, http.StatusNotAcceptable, "unable to marshal response", err) |
| 566 | ct = "application/json" |
| 567 | if ctf, ok := notAccept.(ContentTypeFilter); ok { |
| 568 | ct = ctf.ContentType(ct) |
| 569 | } |
| 570 | |
| 571 | ctx.SetHeader("Content-Type", ct) |
| 572 | if e := transformAndWrite(api, ctx, http.StatusNotAcceptable, "application/json", notAccept); e != nil { |
| 573 | return e |
| 574 | } |
| 575 | |
| 576 | return err |
| 577 | } |
| 578 | |
| 579 | if ctf, ok := body.(ContentTypeFilter); ok { |
| 580 | ct = ctf.ContentType(ct) |
| 581 | } |
| 582 | |
| 583 | ctx.SetHeader("Content-Type", ct) |
| 584 | } |
| 585 | |
| 586 | if err := transformAndWrite(api, ctx, status, ct, body); err != nil { |
| 587 | return err |
| 588 | } |
| 589 | |
| 590 | return nil |
| 591 | } |
| 592 | |
| 593 | func writeResponseWithPanic(api API, ctx Context, status int, ct string, body any) { |
| 594 | if err := writeResponse(api, ctx, status, ct, body); err != nil { |
no test coverage detected
searching dependent graphs…