1package gocb
2
3import (
4	gocbcore "github.com/couchbase/gocbcore/v9"
5	"github.com/pkg/errors"
6)
7
8// HTTPError is the error type of management HTTP errors.
9// UNCOMMITTED: This API may change in the future.
10type HTTPError struct {
11	InnerError    error         `json:"-"`
12	UniqueID      string        `json:"unique_id,omitempty"`
13	Endpoint      string        `json:"endpoint,omitempty"`
14	RetryReasons  []RetryReason `json:"retry_reasons,omitempty"`
15	RetryAttempts uint32        `json:"retry_attempts,omitempty"`
16}
17
18// Error returns the string representation of this error.
19func (e HTTPError) Error() string {
20	return e.InnerError.Error() + " | " + serializeWrappedError(e)
21}
22
23// Unwrap returns the underlying cause for this error.
24func (e HTTPError) Unwrap() error {
25	return e.InnerError
26}
27
28func makeGenericHTTPError(baseErr error, req *gocbcore.HTTPRequest, resp *gocbcore.HTTPResponse) error {
29	if baseErr == nil {
30		logErrorf("makeGenericHTTPError got an empty error")
31		baseErr = errors.New("unknown error")
32	}
33
34	err := HTTPError{
35		InnerError: baseErr,
36	}
37
38	if req != nil {
39		err.UniqueID = req.UniqueID
40	}
41
42	if resp != nil {
43		err.Endpoint = resp.Endpoint
44	}
45
46	return err
47}
48
49func makeGenericMgmtError(baseErr error, req *mgmtRequest, resp *mgmtResponse) error {
50	if baseErr == nil {
51		logErrorf("makeGenericMgmtError got an empty error")
52		baseErr = errors.New("unknown error")
53	}
54
55	err := HTTPError{
56		InnerError: baseErr,
57	}
58
59	if req != nil {
60		err.UniqueID = req.UniqueID
61	}
62
63	if resp != nil {
64		err.Endpoint = resp.Endpoint
65	}
66
67	return err
68}
69
70func makeMgmtBadStatusError(message string, req *mgmtRequest, resp *mgmtResponse) error {
71	return makeGenericMgmtError(errors.New(message), req, resp)
72}
73