forked from Azure/azure-sdk-for-java
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample for handling exceptions (Azure#141)
* Add sample for handling exceptions
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
sdk/search/azure-search/src/samples/java/com/azure/search/HttpResponseExceptionExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
*/ | ||
} |