errors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package client
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "github.com/docker/distribution/registry/api/errcode"
  10. )
  11. // ErrNoErrorsInBody is returned when a HTTP response body parses to an empty
  12. // errcode.Errors slice.
  13. var ErrNoErrorsInBody = errors.New("no error details found in HTTP response body")
  14. // UnexpectedHTTPStatusError is returned when an unexpected HTTP status is
  15. // returned when making a registry api call.
  16. type UnexpectedHTTPStatusError struct {
  17. Status string
  18. }
  19. func (e *UnexpectedHTTPStatusError) Error() string {
  20. return fmt.Sprintf("received unexpected HTTP status: %s", e.Status)
  21. }
  22. // UnexpectedHTTPResponseError is returned when an expected HTTP status code
  23. // is returned, but the content was unexpected and failed to be parsed.
  24. type UnexpectedHTTPResponseError struct {
  25. ParseErr error
  26. StatusCode int
  27. Response []byte
  28. }
  29. func (e *UnexpectedHTTPResponseError) Error() string {
  30. return fmt.Sprintf("error parsing HTTP %d response body: %s: %q", e.StatusCode, e.ParseErr.Error(), string(e.Response))
  31. }
  32. func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
  33. var errors errcode.Errors
  34. body, err := ioutil.ReadAll(r)
  35. if err != nil {
  36. return err
  37. }
  38. // For backward compatibility, handle irregularly formatted
  39. // messages that contain a "details" field.
  40. var detailsErr struct {
  41. Details string `json:"details"`
  42. }
  43. err = json.Unmarshal(body, &detailsErr)
  44. if err == nil && detailsErr.Details != "" {
  45. if statusCode == http.StatusUnauthorized {
  46. return errcode.ErrorCodeUnauthorized.WithMessage(detailsErr.Details)
  47. }
  48. return errcode.ErrorCodeUnknown.WithMessage(detailsErr.Details)
  49. }
  50. if err := json.Unmarshal(body, &errors); err != nil {
  51. return &UnexpectedHTTPResponseError{
  52. ParseErr: err,
  53. StatusCode: statusCode,
  54. Response: body,
  55. }
  56. }
  57. if len(errors) == 0 {
  58. // If there was no error specified in the body, return
  59. // UnexpectedHTTPResponseError.
  60. return &UnexpectedHTTPResponseError{
  61. ParseErr: ErrNoErrorsInBody,
  62. StatusCode: statusCode,
  63. Response: body,
  64. }
  65. }
  66. return errors
  67. }
  68. // HandleErrorResponse returns error parsed from HTTP response for an
  69. // unsuccessful HTTP response code (in the range 400 - 499 inclusive). An
  70. // UnexpectedHTTPStatusError returned for response code outside of expected
  71. // range.
  72. func HandleErrorResponse(resp *http.Response) error {
  73. if resp.StatusCode == 401 {
  74. err := parseHTTPErrorResponse(resp.StatusCode, resp.Body)
  75. if uErr, ok := err.(*UnexpectedHTTPResponseError); ok {
  76. return errcode.ErrorCodeUnauthorized.WithDetail(uErr.Response)
  77. }
  78. return err
  79. }
  80. if resp.StatusCode >= 400 && resp.StatusCode < 500 {
  81. return parseHTTPErrorResponse(resp.StatusCode, resp.Body)
  82. }
  83. return &UnexpectedHTTPStatusError{Status: resp.Status}
  84. }
  85. // SuccessStatus returns true if the argument is a successful HTTP response
  86. // code (in the range 200 - 399 inclusive).
  87. func SuccessStatus(status int) bool {
  88. return status >= 200 && status <= 399
  89. }