-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
64 lines (56 loc) · 1.78 KB
/
error.go
File metadata and controls
64 lines (56 loc) · 1.78 KB
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
54
55
56
57
58
59
60
61
62
63
64
// Package rest provides a client for interacting with RESTful API services.
package rest
import (
"errors"
"fmt"
"io/fs"
"os"
)
// ErrLoginRequired is returned when an API endpoint requires authentication
// but no valid token was provided.
var ErrLoginRequired = errors.New("login required")
// Error represents an error returned by a REST API endpoint.
// It wraps the Response object and provides standard error interfaces.
type Error struct {
Response *Response
parent error
}
// Error returns a string representation of the REST API error.
func (r *Error) Error() string {
if r.Response.RequestID != "" {
return fmt.Sprintf("[rest] error from server: %s (X-Request-Id: %s)", r.Response.Error, r.Response.RequestID)
}
return fmt.Sprintf("[rest] error from server: %s", r.Response.Error)
}
// Unwrap implements the errors.Unwrapper interface to allow error checking with errors.Is.
// It maps REST API errors to standard Go errors where applicable (e.g., 403 to os.ErrPermission).
func (r *Error) Unwrap() error {
if r.parent != nil {
return r.parent
}
// check for various type of errors
switch r.Response.Code {
case 403:
return os.ErrPermission
case 404:
return fs.ErrNotExist
default:
return nil
}
}
// HttpError represents an HTTP transport error that occurred during a REST API request.
// It captures HTTP status codes and response bodies for debugging.
type HttpError struct {
Code int
Body []byte
e error // unwrap error
}
// Error returns a string representation of the HTTP error.
func (e *HttpError) Error() string {
return fmt.Sprintf("HTTP Error %d: %s", e.Code, e.Body)
}
// Unwrap implements the errors.Unwrapper interface to allow error checking with errors.Is.
// It returns the underlying error, if any.
func (e *HttpError) Unwrap() error {
return e.e
}