File: error.go

package info (click to toggle)
golang-github-apptainer-container-key-client 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 164 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,518 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.

package client

import (
	"errors"
	"fmt"
	"net/http"

	jsonresp "github.com/sylabs/json-resp"
)

// HTTPError represents an error returned from an HTTP server.
type HTTPError struct {
	code int
	err  error
}

// Code returns the HTTP status code associated with e.
func (e *HTTPError) Code() int { return e.code }

// Unwrap returns the error wrapped by e.
func (e *HTTPError) Unwrap() error { return e.err }

// Error returns a human-readable representation of e.
func (e *HTTPError) Error() string {
	if e.err != nil {
		return fmt.Sprintf("%v %v: %v", e.code, http.StatusText(e.code), e.err.Error())
	}
	return fmt.Sprintf("%v %v", e.code, http.StatusText(e.code))
}

// Is compares e against target. If target is a HTTPError with the same code as e, true is returned.
func (e *HTTPError) Is(target error) bool {
	t, ok := target.(*HTTPError)
	return ok && (t.code == e.code)
}

// errorFromResponse returns an HTTPError containing the status code and detailed error message (if
// available) from res.
func errorFromResponse(res *http.Response) error {
	httpErr := HTTPError{code: res.StatusCode}

	var jerr *jsonresp.Error
	if err := jsonresp.ReadError(res.Body); errors.As(err, &jerr) {
		httpErr.err = errors.New(jerr.Message)
	}

	return &httpErr
}