Skip to content

Commit 92f7f3b

Browse files
committed
Pass the status code back in the error if we have it
1 parent eafbe2a commit 92f7f3b

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

Sources/ElasticsearchNIOClient/ElasticsearchClient+Requests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import SotoElasticsearchService
55
extension ElasticsearchClient {
66
public func bulk<Document: Encodable>(_ operations: [ESBulkOperation<Document>]) -> EventLoopFuture<ESBulkResponse> {
77
guard operations.count > 0 else {
8-
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No operations to perform for the bulk API"))
8+
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No operations to perform for the bulk API", status: nil))
99
}
1010
do {
1111
let url = try buildURL(path: "/_bulk")
@@ -15,42 +15,42 @@ extension ElasticsearchClient {
1515
switch operation.operationType {
1616
case .create:
1717
guard let document = operation.document else {
18-
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
18+
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
1919
}
2020
let createInfo = BulkCreate(create: bulkOperationBody)
2121
let createLine = try self.jsonEncoder.encode(createInfo)
2222
let dataLine = try self.jsonEncoder.encode(document)
2323
guard let createLineString = String(data: createLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
24-
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
24+
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
2525
}
2626
bodyString.append("\(createLineString)\n\(dataLineString)\n")
2727
case .delete:
2828
let deleteInfo = BulkDelete(delete: bulkOperationBody)
2929
let deleteLine = try self.jsonEncoder.encode(deleteInfo)
3030
guard let deleteLineString = String(data: deleteLine, encoding: .utf8) else {
31-
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
31+
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
3232
}
3333
bodyString.append("\(deleteLineString)\n")
3434
case .index:
3535
guard let document = operation.document else {
36-
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
36+
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
3737
}
3838
let indexInfo = BulkIndex(index: bulkOperationBody)
3939
let indexLine = try self.jsonEncoder.encode(indexInfo)
4040
let dataLine = try self.jsonEncoder.encode(document)
4141
guard let indexLineString = String(data: indexLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
42-
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
42+
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
4343
}
4444
bodyString.append("\(indexLineString)\n\(dataLineString)\n")
4545
case .update:
4646
guard let document = operation.document else {
47-
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
47+
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
4848
}
4949
let updateInfo = BulkUpdate(update: bulkOperationBody)
5050
let updateLine = try self.jsonEncoder.encode(updateInfo)
5151
let dataLine = try self.jsonEncoder.encode(BulkUpdateDocument(doc: document))
5252
guard let updateLineString = String(data: updateLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
53-
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
53+
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
5454
}
5555
bodyString.append("\(updateLineString)\n\(dataLineString)\n")
5656
}
@@ -158,7 +158,7 @@ extension ElasticsearchClient {
158158
let url = try buildURL(path: "/\(name)")
159159
return signAndExecuteRequest(url: url, method: .HEAD, headers: .init(), body: .empty).flatMapThrowing { response in
160160
guard response.status == .ok || response.status == .notFound else {
161-
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)")
161+
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status.code)
162162
}
163163
return response.status == .ok
164164
}

Sources/ElasticsearchNIOClient/ElasticsearchClient.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public struct ElasticsearchClient {
4343
case 200...299:
4444
guard var body = clientResponse.body else {
4545
self.logger.debug("No body from ElasticSearch response")
46-
throw ElasticSearchClientError(message: "No body from ElasticSearch response")
46+
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status.code)
4747
}
4848
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
4949
self.logger.debug("Failed to convert \(D.self)")
50-
throw ElasticSearchClientError(message: "Failed to convert \(D.self)")
50+
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: clientResponse.status.code)
5151
}
5252
return response
5353
default:
@@ -58,15 +58,15 @@ public struct ElasticsearchClient {
5858
responseBody = "Empty"
5959
}
6060
self.logger.trace("Got response status \(clientResponse.status) from ElasticSearch with response \(clientResponse) when trying \(method) request to \(url). Request body was \(body.asString() ?? "Empty") and response body was \(responseBody)")
61-
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch")
61+
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status.code)
6262
}
6363
}
6464
}
6565

6666
func signAndExecuteRequest(url urlString: String, method: HTTPMethod, headers: HTTPHeaders, body: AWSPayload) -> EventLoopFuture<HTTPClient.Response> {
6767
let es = ElasticsearchService(client: awsClient, region: self.region)
6868
guard let url = URL(string: urlString) else {
69-
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "Failed to convert \(urlString) to a URL"))
69+
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "Failed to convert \(urlString) to a URL", status: nil))
7070
}
7171
return es.signHeaders(url: url, httpMethod: method, headers: headers, body: body).flatMap { headers in
7272
let request: HTTPClient.Request
@@ -98,7 +98,7 @@ extension ElasticsearchClient {
9898
urlComponents.queryItems = queryItems
9999
guard let url = urlComponents.url else {
100100
self.logger.debug("malformed url: \(urlComponents)")
101-
throw ElasticSearchClientError(message: "malformed url: \(urlComponents)")
101+
throw ElasticSearchClientError(message: "malformed url: \(urlComponents)", status: nil)
102102
}
103103
return url.absoluteString
104104
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
public struct ElasticSearchClientError: Error {
22
public let message: String
3+
public let status: UInt?
34
}

0 commit comments

Comments
 (0)