code.gitea.io/gitea@v1.21.7/routers/api/packages/container/errors.go (about)

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