diff --git a/sdk/search/azure-search/src/samples/java/com/azure/search/HttpResponseExceptionExample.java b/sdk/search/azure-search/src/samples/java/com/azure/search/HttpResponseExceptionExample.java new file mode 100644 index 0000000000000..e193b903ccee4 --- /dev/null +++ b/sdk/search/azure-search/src/samples/java/com/azure/search/HttpResponseExceptionExample.java @@ -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 = ""; + String 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 = ""; + String 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(); + } + */ +}