...

Source file src/github.com/google/go-containerregistry/pkg/registry/error.go

Documentation: github.com/google/go-containerregistry/pkg/registry

     1  // Copyright 2018 Google LLC All Rights Reserved.
     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 registry
    16  
    17  import (
    18  	"encoding/json"
    19  	"net/http"
    20  )
    21  
    22  type regError struct {
    23  	Status  int
    24  	Code    string
    25  	Message string
    26  }
    27  
    28  func (r *regError) Write(resp http.ResponseWriter) error {
    29  	resp.WriteHeader(r.Status)
    30  
    31  	type err struct {
    32  		Code    string `json:"code"`
    33  		Message string `json:"message"`
    34  	}
    35  	type wrap struct {
    36  		Errors []err `json:"errors"`
    37  	}
    38  	return json.NewEncoder(resp).Encode(wrap{
    39  		Errors: []err{
    40  			{
    41  				Code:    r.Code,
    42  				Message: r.Message,
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  // regErrInternal returns an internal server error.
    49  func regErrInternal(err error) *regError {
    50  	return &regError{
    51  		Status:  http.StatusInternalServerError,
    52  		Code:    "INTERNAL_SERVER_ERROR",
    53  		Message: err.Error(),
    54  	}
    55  }
    56  
    57  var regErrBlobUnknown = &regError{
    58  	Status:  http.StatusNotFound,
    59  	Code:    "BLOB_UNKNOWN",
    60  	Message: "Unknown blob",
    61  }
    62  
    63  var regErrUnsupported = &regError{
    64  	Status:  http.StatusMethodNotAllowed,
    65  	Code:    "UNSUPPORTED",
    66  	Message: "Unsupported operation",
    67  }
    68  
    69  var regErrDigestMismatch = &regError{
    70  	Status:  http.StatusBadRequest,
    71  	Code:    "DIGEST_INVALID",
    72  	Message: "digest does not match contents",
    73  }
    74  
    75  var regErrDigestInvalid = &regError{
    76  	Status:  http.StatusBadRequest,
    77  	Code:    "NAME_INVALID",
    78  	Message: "invalid digest",
    79  }
    80  

View as plain text