...

Source file src/cuelabs.dev/go/oci/ociregistry/error.go

Documentation: cuelabs.dev/go/oci/ociregistry

     1  // Copyright 2023 CUE Labs AG
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ociregistry
    16  
    17  // NewError returns a new error with the given code, message and detail.
    18  func NewError(msg string, code string, detail any) Error {
    19  	return &registryError{
    20  		code:    code,
    21  		message: msg,
    22  		detail:  detail,
    23  	}
    24  }
    25  
    26  // Error represents an OCI registry error. The set of codes is defined
    27  // in the [distribution specification].
    28  //
    29  // [distribution specification]: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#error-codes
    30  type Error interface {
    31  	// error.Error provides the error message.
    32  	error
    33  
    34  	// Code returns the error code. See
    35  	Code() string
    36  
    37  	// Detail returns any detail to be associated with the error; it should
    38  	// be JSON-marshable.
    39  	Detail() any
    40  }
    41  
    42  // The following values represent the known error codes.
    43  var (
    44  	ErrBlobUnknown         = NewError("blob unknown to registry", "BLOB_UNKNOWN", nil)
    45  	ErrBlobUploadInvalid   = NewError("blob upload invalid", "BLOB_UPLOAD_INVALID", nil)
    46  	ErrBlobUploadUnknown   = NewError("blob upload unknown to registry", "BLOB_UPLOAD_UNKNOWN", nil)
    47  	ErrDigestInvalid       = NewError("provided digest did not match uploaded content", "DIGEST_INVALID", nil)
    48  	ErrManifestBlobUnknown = NewError("manifest references a manifest or blob unknown to registry", "MANIFEST_BLOB_UNKNOWN", nil)
    49  	ErrManifestInvalid     = NewError("manifest invalid", "MANIFEST_INVALID", nil)
    50  	ErrManifestUnknown     = NewError("manifest unknown to registry", "MANIFEST_UNKNOWN", nil)
    51  	ErrNameInvalid         = NewError("invalid repository name", "NAME_INVALID", nil)
    52  	ErrNameUnknown         = NewError("repository name not known to registry", "NAME_UNKNOWN", nil)
    53  	ErrSizeInvalid         = NewError("provided length did not match content length", "SIZE_INVALID", nil)
    54  	ErrUnauthorized        = NewError("authentication required", "UNAUTHORIZED", nil)
    55  	ErrDenied              = NewError("requested access to the resource is denied", "DENIED", nil)
    56  	ErrUnsupported         = NewError("the operation is unsupported", "UNSUPPORTED", nil)
    57  	ErrTooManyRequests     = NewError("too many requests", "TOOMANYREQUESTS", nil)
    58  
    59  	// ErrRangeInvalid allows Interface implementations to reject invalid ranges,
    60  	// such as a chunked upload PATCH not following the range from a previous PATCH.
    61  	// ociserver relies on this error to return 416 HTTP status codes.
    62  	//
    63  	// It is separate from the Error type since the spec only dictates its HTTP status code,
    64  	// but does not assign any error code to it.
    65  	// We borrowed RANGE_INVALID from the Docker registry implementation, a de facto standard.
    66  	ErrRangeInvalid = NewError("invalid content range", "RANGE_INVALID", nil)
    67  )
    68  
    69  type registryError struct {
    70  	code    string
    71  	message string
    72  	detail  any
    73  }
    74  
    75  func (e *registryError) Code() string {
    76  	return e.code
    77  }
    78  
    79  func (e *registryError) Error() string {
    80  	return e.message
    81  }
    82  
    83  func (e *registryError) Detail() any {
    84  	return e.detail
    85  }
    86  

View as plain text