Debian Codegraph @

errors.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "context"
9 "encoding/json"
10 "fmt"
11 "io/ioutil"
12 "net/http"
13 "net/url"
14
15 "github.com/pkg/errors"
16 )
17
18 // checkResponse will return an error if the request/response indicates
19 // an error returned from Elasticsearch.
20 //
21 // HTTP status codes between in the range [200..299] are considered successful.
22 // All other errors are considered errors except they are specified in
23 // ignoreErrors. This is necessary because for some services, HTTP status 404
24 // is a valid response from Elasticsearch (e.g. the Exists service).
25 //
26 // The func tries to parse error details as returned from Elasticsearch
27 // and encapsulates them in type elastic.Error.
28 func checkResponse(req *http.Request, res *http.Response, ignoreErrors ...int) error {
29 // 200-299 are valid status codes
30 if res.StatusCode >= 200 && res.StatusCode <= 299 {
31 return nil
32 }
33 // Ignore certain errors?
34 for _, code := range ignoreErrors {
35 if code == res.StatusCode {
36 return nil
37 }
38 }
39 return createResponseError(res)
40 }
41
42 // createResponseError creates an Error structure from the HTTP response,
43 // its status code and the error information sent by Elasticsearch.
44 func createResponseError(res *http.Response) error {
45 if res.Body == nil {
46 return &Error{Status: res.StatusCode}
47 }
48 data, err := ioutil.ReadAll(res.Body)
49 if err != nil {
50 return &Error{Status: res.StatusCode}
51 }
52 errReply := new(Error)
53 err = json.Unmarshal(data, errReply)
54 if err != nil {
55 return &Error{Status: res.StatusCode}
56 }
57 if errReply != nil {
58 if errReply.Status == 0 {
59 errReply.Status = res.StatusCode
60 }
61 return errReply
62 }
63 return &Error{Status: res.StatusCode}
64 }
65
66 // Error encapsulates error details as returned from Elasticsearch.
67 type Error struct {
68 Status int `json:"status"`
69 Details *ErrorDetails `json:"error,omitempty"`
70 }
71
72 // ErrorDetails encapsulate error details from Elasticsearch.
73 // It is used in e.g. elastic.Error and elastic.BulkResponseItem.
74 type ErrorDetails struct {
75 Type string `json:"type"`
76 Reason string `json:"reason"`
77 ResourceType string `json:"resource.type,omitempty"`
78 ResourceId string `json:"resource.id,omitempty"`
79 Index string `json:"index,omitempty"`
80 Phase string `json:"phase,omitempty"`
81 Grouped bool `json:"grouped,omitempty"`
82 CausedBy map[string]interface{} `json:"caused_by,omitempty"`
83 RootCause []*ErrorDetails `json:"root_cause,omitempty"`
84 FailedShards []map[string]interface{} `json:"failed_shards,omitempty"`
85 }
86
87 // Error returns a string representation of the error.
88 func (e *Error) Error() string {
89 if e.Details != nil && e.Details.Reason != "" {
90 return fmt.Sprintf("elastic: Error %d (%s): %s [type=%s]", e.Status, http.StatusText(e.Status), e.Details.Reason, e.Details.Type)
91 }
92 return fmt.Sprintf("elastic: Error %d (%s)", e.Status, http.StatusText(e.Status))
93 }
94
95 // IsContextErr returns true if the error is from a context that was canceled or deadline exceeded
96 func IsContextErr(err error) bool {
97 if err == context.Canceled || err == context.DeadlineExceeded {
98 return true
99 }
100 // This happens e.g. on redirect errors, see https://golang.org/src/net/http/client_test.go#L329
101 if ue, ok := err.(*url.Error); ok {
102 if ue.Temporary() {
103 return true
104 }
105 // Use of an AWS Signing Transport can result in a wrapped url.Error
106 return IsContextErr(ue.Err)
107 }
108 return false
109 }
110
111 // IsConnErr returns true if the error indicates that Elastic could not
112 // find an Elasticsearch host to connect to.
113 func IsConnErr(err error) bool {
114 return err == ErrNoClient || errors.Cause(err) == ErrNoClient
115 }
116
117 // IsNotFound returns true if the given error indicates that Elasticsearch
118 // returned HTTP status 404. The err parameter can be of type *elastic.Error,
119 // elastic.Error, *http.Response or int (indicating the HTTP status code).
120 func IsNotFound(err interface{}) bool {
121 return IsStatusCode(err, http.StatusNotFound)
122 }
123
124 // IsTimeout returns true if the given error indicates that Elasticsearch
125 // returned HTTP status 408. The err parameter can be of type *elastic.Error,
126 // elastic.Error, *http.Response or int (indicating the HTTP status code).
127 func IsTimeout(err interface{}) bool {
128 return IsStatusCode(err, http.StatusRequestTimeout)
129 }
130
131 // IsConflict returns true if the given error indicates that the Elasticsearch
132 // operation resulted in a version conflict. This can occur in operations like
133 // `update` or `index` with `op_type=create`. The err parameter can be of
134 // type *elastic.Error, elastic.Error, *http.Response or int (indicating the
135 // HTTP status code).
136 func IsConflict(err interface{}) bool {
137 return IsStatusCode(err, http.StatusConflict)
138 }
139
140 // IsStatusCode returns true if the given error indicates that the Elasticsearch
141 // operation returned the specified HTTP status code. The err parameter can be of
142 // type *http.Response, *Error, Error, or int (indicating the HTTP status code).
143 func IsStatusCode(err interface{}, code int) bool {
144 switch e := err.(type) {
145 case *http.Response:
146 return e.StatusCode == code
147 case *Error:
148 return e.Status == code
149 case Error:
150 return e.Status == code
151 case int:
152 return e == code
153 }
154 return false
155 }
156
157 // -- General errors --
158
159 // ShardsInfo represents information from a shard.
160 type ShardsInfo struct {
161 Total int `json:"total"`
162 Successful int `json:"successful"`
163 Failed int `json:"failed"`
164 Failures []*ShardFailure `json:"failures,omitempty"`
165 }
166
167 // ShardFailure represents details about a failure.
168 type ShardFailure struct {
169 Index string `json:"_index,omitempty"`
170 Shard int `json:"_shard,omitempty"`
171 Node string `json:"_node,omitempty"`
172 Reason map[string]interface{} `json:"reason,omitempty"`
173 Status string `json:"status,omitempty"`
174 Primary bool `json:"primary,omitempty"`
175 }