gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package container
  4. import (
  5. "net/http"
  6. )
  7. // https://github.com/opencontainers/distribution-spec/blob/main/spec.md#error-codes
  8. var (
  9. errBlobUnknown = &namedError{Code: "BLOB_UNKNOWN", StatusCode: http.StatusNotFound}
  10. errBlobUploadInvalid = &namedError{Code: "BLOB_UPLOAD_INVALID", StatusCode: http.StatusBadRequest}
  11. errBlobUploadUnknown = &namedError{Code: "BLOB_UPLOAD_UNKNOWN", StatusCode: http.StatusNotFound}
  12. errDigestInvalid = &namedError{Code: "DIGEST_INVALID", StatusCode: http.StatusBadRequest}
  13. errManifestBlobUnknown = &namedError{Code: "MANIFEST_BLOB_UNKNOWN", StatusCode: http.StatusNotFound}
  14. errManifestInvalid = &namedError{Code: "MANIFEST_INVALID", StatusCode: http.StatusBadRequest}
  15. errManifestUnknown = &namedError{Code: "MANIFEST_UNKNOWN", StatusCode: http.StatusNotFound}
  16. errNameInvalid = &namedError{Code: "NAME_INVALID", StatusCode: http.StatusBadRequest}
  17. errNameUnknown = &namedError{Code: "NAME_UNKNOWN", StatusCode: http.StatusNotFound}
  18. errSizeInvalid = &namedError{Code: "SIZE_INVALID", StatusCode: http.StatusBadRequest}
  19. errUnauthorized = &namedError{Code: "UNAUTHORIZED", StatusCode: http.StatusUnauthorized}
  20. errUnsupported = &namedError{Code: "UNSUPPORTED", StatusCode: http.StatusNotImplemented}
  21. )
  22. type namedError struct {
  23. Code string
  24. StatusCode int
  25. Message string
  26. }
  27. func (e *namedError) Error() string {
  28. return e.Message
  29. }
  30. // WithMessage creates a new instance of the error with a different message
  31. func (e *namedError) WithMessage(message string) *namedError {
  32. return &namedError{
  33. Code: e.Code,
  34. StatusCode: e.StatusCode,
  35. Message: message,
  36. }
  37. }
  38. // WithStatusCode creates a new instance of the error with a different status code
  39. func (e *namedError) WithStatusCode(statusCode int) *namedError {
  40. return &namedError{
  41. Code: e.Code,
  42. StatusCode: statusCode,
  43. Message: e.Message,
  44. }
  45. }