Writes the response status code, and if it's an error writes a JSON description to the body.
(status int, message string)
| 1683 | |
| 1684 | // Writes the response status code, and if it's an error writes a JSON description to the body. |
| 1685 | func (h *handler) writeStatus(status int, message string) { |
| 1686 | if status < 300 { |
| 1687 | h.response.WriteHeader(status) |
| 1688 | h.setStatus(status, message) |
| 1689 | return |
| 1690 | } |
| 1691 | // Got an error: |
| 1692 | var errorStr string |
| 1693 | switch status { |
| 1694 | case http.StatusNotFound: |
| 1695 | errorStr = "not_found" |
| 1696 | case http.StatusConflict: |
| 1697 | errorStr = "conflict" |
| 1698 | default: |
| 1699 | errorStr = http.StatusText(status) |
| 1700 | if errorStr == "" { |
| 1701 | errorStr = fmt.Sprintf("%d", status) |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | h.disableResponseCompression() |
| 1706 | h.setHeader("Content-Type", "application/json") |
| 1707 | h.response.WriteHeader(status) |
| 1708 | h.setStatus(status, message) |
| 1709 | |
| 1710 | _, _ = h.response.Write([]byte(`{"error":"` + errorStr + `","reason":` + base.ConvertToJSONString(message) + `}`)) |
| 1711 | } |
| 1712 | |
| 1713 | var kRangeRegex = regexp.MustCompile("^bytes=(\\d+)?-(\\d+)?$") |
| 1714 |
no test coverage detected