| 1 | /* |
| 2 | Copyright The ORAS Authors. |
| 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 | |
| 16 | package errutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/internal/errutil`/ |
| 17 | |
| 18 | import ( |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 21 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 22 | "net/httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/" |
| 23 | |
| 24 | "oras.land/oras-go/v2/registry/remote/errcodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/" |
| 25 | ) |
| 26 | |
| 27 | // maxErrorBytes specifies the default limit on how many response bytes are |
| 28 | // allowed in the server's error response. |
| 29 | // A typical error message is around 200 bytes. Hence, 8 KiB should be |
| 30 | // sufficient. |
| 31 | const maxErrorBytesscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/internal/errutil`/maxErrorBytes. int64 = 8 * 1024 // 8 KiB |
| 32 | |
| 33 | // ParseErrorResponse parses the error returned by the remote registry. |
| 34 | func ParseErrorResponsescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/internal/errutil`/ParseErrorResponse().(resp *httpscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#) error { |
| 35 | resultErr := &errcodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/.ErrorResponsescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/ErrorResponse#{ |
| 36 | Methodscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/ErrorResponse#Method.: resp.Requestscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Request..Methodscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Request#Method., |
| 37 | URLscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/ErrorResponse#URL.: resp.Requestscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Request..URLscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Request#URL., |
| 38 | StatusCodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/ErrorResponse#StatusCode.: resp.StatusCodescip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#StatusCode., |
| 39 | } |
| 40 | var body struct { |
| 41 | Errors errcodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/.Errorsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/Errors# `json:"errors"` |
| 42 | } |
| 43 | lr := ioscip-go gomod github.com/golang/go/src go1.23.0 io/.LimitReaderscip-go gomod github.com/golang/go/src go1.23.0 io/LimitReader().(resp.Bodyscip-go gomod github.com/golang/go/src go1.23.0 `net/http`/Response#Body., maxErrorBytesscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/internal/errutil`/maxErrorBytes.) |
| 44 | if err := jsonscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/NewDecoder().(lr).Decodescip-go gomod github.com/golang/go/src go1.23.0 `encoding/json`/Decoder#Decode().(&body); err == nil { |
| 45 | resultErr.Errorsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/ErrorResponse#Errors. = body.Errors |
| 46 | } |
| 47 | return resultErr |
| 48 | } |
| 49 | |
| 50 | // IsErrorCode returns true if err is an Error and its Code equals to code. |
| 51 | func IsErrorCodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/internal/errutil`/IsErrorCode().(err error, code string) bool { |
| 52 | var ec errcodescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/.Errorscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/Error# |
| 53 | return errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Asscip-go gomod github.com/golang/go/src go1.23.0 errors/As().(err, &ec) && ec.Codescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/errcode`/Error#Code. == code |
| 54 | } |