MCPcopy Index your code
hub / github.com/coder/coder / readRawBodyAsError

Function readRawBodyAsError

codersdk/chats.go:1852–1884  ·  view source on GitHub ↗
(res *http.Response, rawBody []byte)

Source from the content-addressed store, hash-verified

1850}
1851
1852func readRawBodyAsError(res *http.Response, rawBody []byte) error {
1853 if mimeErr := ExpectJSONMime(res); mimeErr != nil {
1854 if len(rawBody) > 2048 {
1855 rawBody = append(rawBody[:2048], []byte("...")...)
1856 }
1857 if len(rawBody) == 0 {
1858 rawBody = []byte("no response body")
1859 }
1860 return newResponseError(res, Response{
1861 Message: mimeErr.Error(),
1862 Detail: string(rawBody),
1863 })
1864 }
1865
1866 var response Response
1867 if err := json.NewDecoder(bytes.NewReader(rawBody)).Decode(&response); err != nil {
1868 if errors.Is(err, io.EOF) {
1869 return newResponseError(res, Response{Message: "empty response body"})
1870 }
1871 return xerrors.Errorf("decode body: %w", err)
1872 }
1873 if response.Message == "" {
1874 if len(rawBody) > 1024 {
1875 rawBody = append(rawBody[:1024], []byte("...")...)
1876 }
1877 response.Message = fmt.Sprintf(
1878 "unexpected status code %d, response has no message",
1879 res.StatusCode,
1880 )
1881 response.Detail = string(rawBody)
1882 }
1883 return newResponseError(res, response)
1884}
1885
1886func newResponseError(res *http.Response, response Response) *Error {
1887 if res == nil {

Callers 1

Calls 5

ExpectJSONMimeFunction · 0.85
newResponseErrorFunction · 0.70
ErrorMethod · 0.45
IsMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected