Skip to content

Commit

Permalink
Add sample for handling exceptions (Azure#141)
Browse files Browse the repository at this point in the history
* Add sample for handling exceptions
  • Loading branch information
sakintoye committed Oct 14, 2019
1 parent 9794559 commit 8a78970
Showing 1 changed file with 91 additions and 0 deletions.
@@ -0,0 +1,91 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search;

import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpResponse;
import com.azure.search.models.SearchParameters;

/**
* An example that demonstrates how to read exceptions when a
* Search Service returns a non successful response (3xx/4xx/5xx).
*
* This sample makes search request to the service,
* applying a "filter" expression containing an invalid index field.
*
* NOTE: It is assumed you have access to an Azure Search service
* and have created an index called "hotels"
*/
public class HttpResponseExceptionExample {
public static void main(String[] args) {
search();
}

private static void search() {
SearchIndexClient searchClient = getSearchClient();
try {
SearchParameters searchParams = new SearchParameters()
.setFilter("Non_Existent_Field eq 'Luxury'");

searchClient.search("hotel", searchParams, null);
}
catch (HttpResponseException ex) {
HttpResponse response = ex.getResponse();
System.out.println("Status Code: " + response.getStatusCode());
System.out.println("Message: " + response.getBodyAsString().block());
}
}

private static SearchIndexClient getSearchClient() {
String apiKey = "<apiKey>";
String searchServiceName = "<searchServiceName>";

String dnsSuffix = "search.windows.net";
String indexName = "hotels";

ApiKeyCredentials apiKeyCredentials = new ApiKeyCredentials(apiKey);
return new SearchIndexClientBuilder()
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.credential(apiKeyCredentials)
.buildClient();
}

/**
* Async example
* The commented block below demonstrates an Async example of handling Http
*/

/*
private static void asyncSearch() {
SearchIndexAsyncClient client = getSearchAsyncClient();
SearchParameters searchParams = new SearchParameters()
.setFilter("Non_Existent_Field eq 'Luxury'");
client.search("hotel", searchParams, null)
.doOnError(e -> {
HttpResponse response = ((HttpResponseException) e).getResponse();
System.out.println("Status Code: " + response.getStatusCode());
System.out.println("Message: " + response.getBodyAsString().block());
});
}
private static SearchIndexAsyncClient getSearchAsyncClient() {
String apiKey = "<apiKey>";
String searchServiceName = "<searchServiceName>";
String dnsSuffix = "search.windows.net";
String indexName = "hotels";
ApiKeyCredentials apiKeyCredentials = new ApiKeyCredentials(apiKey);
return new SearchIndexClientBuilder()
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.credential(apiKeyCredentials)
.buildAsyncClient();
}
*/
}

0 comments on commit 8a78970

Please sign in to comment.