Skip to content

Commit

Permalink
Pass the status code back in the error if we have it
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Mar 31, 2021
1 parent eafbe2a commit 92f7f3b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SotoElasticsearchService
extension ElasticsearchClient {
public func bulk<Document: Encodable>(_ operations: [ESBulkOperation<Document>]) -> EventLoopFuture<ESBulkResponse> {
guard operations.count > 0 else {
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No operations to perform for the bulk API"))
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No operations to perform for the bulk API", status: nil))
}
do {
let url = try buildURL(path: "/_bulk")
Expand All @@ -15,42 +15,42 @@ extension ElasticsearchClient {
switch operation.operationType {
case .create:
guard let document = operation.document else {
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
}
let createInfo = BulkCreate(create: bulkOperationBody)
let createLine = try self.jsonEncoder.encode(createInfo)
let dataLine = try self.jsonEncoder.encode(document)
guard let createLineString = String(data: createLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
}
bodyString.append("\(createLineString)\n\(dataLineString)\n")
case .delete:
let deleteInfo = BulkDelete(delete: bulkOperationBody)
let deleteLine = try self.jsonEncoder.encode(deleteInfo)
guard let deleteLineString = String(data: deleteLine, encoding: .utf8) else {
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
}
bodyString.append("\(deleteLineString)\n")
case .index:
guard let document = operation.document else {
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
}
let indexInfo = BulkIndex(index: bulkOperationBody)
let indexLine = try self.jsonEncoder.encode(indexInfo)
let dataLine = try self.jsonEncoder.encode(document)
guard let indexLineString = String(data: indexLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
}
bodyString.append("\(indexLineString)\n\(dataLineString)\n")
case .update:
guard let document = operation.document else {
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation"))
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "No document provided for create bulk operation", status: nil))
}
let updateInfo = BulkUpdate(update: bulkOperationBody)
let updateLine = try self.jsonEncoder.encode(updateInfo)
let dataLine = try self.jsonEncoder.encode(BulkUpdateDocument(doc: document))
guard let updateLineString = String(data: updateLine, encoding: .utf8), let dataLineString = String(data: dataLine, encoding: .utf8) else {
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String")
throw ElasticSearchClientError(message: "Failed to convert bulk data from Data to String", status: nil)
}
bodyString.append("\(updateLineString)\n\(dataLineString)\n")
}
Expand Down Expand Up @@ -158,7 +158,7 @@ extension ElasticsearchClient {
let url = try buildURL(path: "/\(name)")
return signAndExecuteRequest(url: url, method: .HEAD, headers: .init(), body: .empty).flatMapThrowing { response in
guard response.status == .ok || response.status == .notFound else {
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)")
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status.code)
}
return response.status == .ok
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/ElasticsearchNIOClient/ElasticsearchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public struct ElasticsearchClient {
case 200...299:
guard var body = clientResponse.body else {
self.logger.debug("No body from ElasticSearch response")
throw ElasticSearchClientError(message: "No body from ElasticSearch response")
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status.code)
}
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
self.logger.debug("Failed to convert \(D.self)")
throw ElasticSearchClientError(message: "Failed to convert \(D.self)")
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: clientResponse.status.code)
}
return response
default:
Expand All @@ -58,15 +58,15 @@ public struct ElasticsearchClient {
responseBody = "Empty"
}
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)")
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch")
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status.code)
}
}
}

func signAndExecuteRequest(url urlString: String, method: HTTPMethod, headers: HTTPHeaders, body: AWSPayload) -> EventLoopFuture<HTTPClient.Response> {
let es = ElasticsearchService(client: awsClient, region: self.region)
guard let url = URL(string: urlString) else {
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "Failed to convert \(urlString) to a URL"))
return self.eventLoop.makeFailedFuture(ElasticSearchClientError(message: "Failed to convert \(urlString) to a URL", status: nil))
}
return es.signHeaders(url: url, httpMethod: method, headers: headers, body: body).flatMap { headers in
let request: HTTPClient.Request
Expand Down Expand Up @@ -98,7 +98,7 @@ extension ElasticsearchClient {
urlComponents.queryItems = queryItems
guard let url = urlComponents.url else {
self.logger.debug("malformed url: \(urlComponents)")
throw ElasticSearchClientError(message: "malformed url: \(urlComponents)")
throw ElasticSearchClientError(message: "malformed url: \(urlComponents)", status: nil)
}
return url.absoluteString
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
public struct ElasticSearchClientError: Error {
public let message: String
public let status: UInt?
}

0 comments on commit 92f7f3b

Please sign in to comment.