Response errorResponse = e.getResponse(); if (errorResponse.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST) { JsonNode root = objectMapper.readTree(EntityUtils.toString(errorResponse.getEntity()));
/** * Create an index * @param index index name * @param ignoreErrors don't fail if the index already exists * @param indexSettings index settings if any * @throws IOException In case of error */ public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException { logger.debug("create index [{}]", index); logger.trace("index settings: [{}]", indexSettings); try { Response response = lowLevelClient.performRequest("PUT", "/" + index, Collections.emptyMap(), createEntity(indexSettings)); logger.trace("create index response: {}", asMap(response)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 400 && (e.getMessage().contains("index_already_exists_exception") || // ES 5.x e.getMessage().contains("IndexAlreadyExistsException") )) { // ES 1.x and 2.x if (!ignoreErrors) { throw new RuntimeException("index already exists"); } logger.trace("index already exists. Ignoring error..."); return; } throw e; } }
public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } // get the POST body String postBody = session.getQueryParameterString(); // or you can access the POST request's parameters String postParameter = session.getParms().get("parameter"); return new Response(postBody); // Or postParameter. }
if (re.getResponse().getStatusLine().getStatusCode() == 404) { logger.warn("Resource for index '{}' not found on action '{}'! {}", index, operationName, re.getLocalizedMessage()); return (T) null; } else if (re.getResponse().getStatusLine().getStatusCode() == 400) { logger.warn("Bad request for index '{}' on action '{}'! {}", index, operationName, re.getLocalizedMessage()); return (T) null; } else {
/** * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}. * If a response body was returned, tries to parse it as an error returned from Elasticsearch. * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException} * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing. */ protected final ElasticsearchStatusException parseResponseException(ResponseException responseException) { Response response = responseException.getResponse(); HttpEntity entity = response.getEntity(); ElasticsearchStatusException elasticsearchException; if (entity == null) { elasticsearchException = new ElasticsearchStatusException( responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException); } else { try { elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent); elasticsearchException.addSuppressed(responseException); } catch (Exception e) { RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode()); elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException); elasticsearchException.addSuppressed(e); } } return elasticsearchException; }
throw new ResponseException((ResponseException) exception);
public ResponseException(Response response) throws IOException { super(buildMessage(response)); this.response = response; }
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage());
@Override public void completed(HttpResponse httpResponse) { try { RequestLogger.logResponse(logger, request, host, httpResponse); int statusCode = httpResponse.getStatusLine().getStatusCode(); Response response = new Response(request.getRequestLine(), host, httpResponse); if (isSuccessfulResponse(statusCode) || ignoreErrorCodes.contains(response.getStatusLine().getStatusCode())) { onResponse(host); listener.onSuccess(response); } else { ResponseException responseException = new ResponseException(response); if (isRetryStatus(statusCode)) { //mark host dead and retry against next one onFailure(host); retryIfPossible(responseException); } else { //mark host alive and don't retry, as the error should be a request problem onResponse(host); listener.onDefinitiveFailure(responseException); } } } catch(Exception e) { listener.onDefinitiveFailure(e); } }
public ResponseException(Response response) throws IOException { super(buildMessage(response)); this.response = response; }
@Override public void deleteIndex(String index) throws IOException { try { Response response = lowLevelClient.performRequest("DELETE", "/" + index); logger.trace("delete index response: {}", asMap(response)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.trace("index does not exist. Ignoring error..."); return; } throw e; } }
/** * Wrap a {@linkplain ResponseException} with another one with the current * stack trace. This is used during synchronous calls so that the caller * ends up in the stack trace of the exception thrown. */ ResponseException(ResponseException e) throws IOException { super(e.getMessage(), e); this.response = e.getResponse(); }
return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); Log.e("UnsupportedEncodingException", re.getMessage());
@Override public void completed(HttpResponse httpResponse) { try { RequestLogger.logResponse(logger, request, host, httpResponse); int statusCode = httpResponse.getStatusLine().getStatusCode(); Response response = new Response(request.getRequestLine(), host, httpResponse); if (isSuccessfulResponse(statusCode) || ignoreErrorCodes.contains(response.getStatusLine().getStatusCode())) { onResponse(host); listener.onSuccess(response); } else { ResponseException responseException = new ResponseException(response); if (isRetryStatus(statusCode)) { //mark host dead and retry against next one onFailure(host); retryIfPossible(responseException); } else { //mark host alive and don't retry, as the error should be a request problem onResponse(host); listener.onDefinitiveFailure(responseException); } } } catch(Exception e) { listener.onDefinitiveFailure(e); } }
public ResponseException(Response response) throws IOException { super(buildMessage(response)); this.response = response; }
/** * Check if an index exists * @param index index name * @return true if the index exists, false otherwise * @throws IOException In case of error */ public boolean isExistingIndex(String index) throws IOException { logger.debug("is existing index [{}]", index); try { Response restResponse = lowLevelClient.performRequest("GET", "/" + index); logger.trace("get index metadata response: {}", asMap(restResponse)); return true; } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug("index [{}] does not exist", index); return false; } throw e; } }
private void handleResponseException(ResponseException e) throws DataException { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug(e.getMessage()); } else { throw new DataException(e); } } }
return getResponse("Internal Error IO Exception: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
@Override public void completed(HttpResponse httpResponse) { try { RequestLogger.logResponse(logger, request, node.getHost(), httpResponse); int statusCode = httpResponse.getStatusLine().getStatusCode(); Response response = new Response(request.getRequestLine(), node.getHost(), httpResponse); if (isSuccessfulResponse(statusCode) || ignoreErrorCodes.contains(response.getStatusLine().getStatusCode())) { onResponse(node); if (strictDeprecationMode && response.hasWarnings()) { listener.onDefinitiveFailure(new ResponseException(response)); } else { listener.onSuccess(response); } } else { ResponseException responseException = new ResponseException(response); if (isRetryStatus(statusCode)) { //mark host dead and retry against next one onFailure(node); retryIfPossible(responseException); } else { //mark host alive and don't retry, as the error should be a request problem onResponse(node); listener.onDefinitiveFailure(responseException); } } } catch(Exception e) { listener.onDefinitiveFailure(e); } }
/** * Check if a pipeline exists * @param pipelineName pipeline name * @return true if the pipeline exists, false otherwise * @throws IOException In case of error */ public boolean isExistingPipeline(String pipelineName) throws IOException { logger.debug("is existing pipeline [{}]", pipelineName); try { Response restResponse = lowLevelClient.performRequest("GET", "/_ingest/pipeline/" + pipelineName); logger.trace("get pipeline metadata response: {}", asMap(restResponse)); return true; } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug("pipeline [{}] does not exist", pipelineName); return false; } throw e; } }