diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index b12e7fdd2bea..2bf6c47591d4 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -3,8 +3,8 @@ ## 1.0.0-beta.3 (Unreleased) - Rename parameters data and sourceUrl parameters found on methods for FormRecognizerClient to form and formUrl, respectively. - Rename parameters for receipt API methods to receipt and receiptUrl. -- Raise `HttpResponseException` when a model with `ModelStatus.Invalid` is returned from the `beginTraining()` API's -- Fix `HttpResponseException` to include the error object thrown on invalid analyze status for recognize API's +- Raise `FormRecognizerException` when a model with `ModelStatus.Invalid` is returned from the `beginTraining()` API's +- Raise `FormRecognizerException` when invalid analyze status returned from service for recognize API's - Update FormField property `transactionTime` on `USReceipt` to return `LocalTime` instead of `String` - Rename model `PageRange` to `FormPageRange` - Rename property `startPageNumber` to `firstPageNumber` and `endPageNumber` to `lastPageNumber` in model `PageRange` diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java index daae4ed77bb1..7c1d2d7009d3 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java @@ -6,11 +6,12 @@ import com.azure.ai.formrecognizer.implementation.FormRecognizerClientImpl; import com.azure.ai.formrecognizer.implementation.models.AnalyzeOperationResult; import com.azure.ai.formrecognizer.implementation.models.ContentType; -import com.azure.ai.formrecognizer.models.ErrorInformation; import com.azure.ai.formrecognizer.implementation.models.OperationStatus; import com.azure.ai.formrecognizer.implementation.models.SourcePath; +import com.azure.ai.formrecognizer.models.ErrorInformation; import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.FormPage; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.RecognizedReceipt; @@ -86,7 +87,7 @@ public final class FormRecognizerAsyncClient { * * @return A {@link PollerFlux} that polls the recognize custom form operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl}, {@code modelId} is {@code null}. */ @@ -113,7 +114,7 @@ public final class FormRecognizerAsyncClient { * * @return A {@link PollerFlux} that polls the recognize custom form operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl}, {@code modelId} is {@code null}. */ @@ -149,7 +150,7 @@ public final class FormRecognizerAsyncClient { * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form}, {@code modelId} is {@code null}. */ @@ -181,7 +182,7 @@ public final class FormRecognizerAsyncClient { * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form}, {@code modelId} is {@code null}. */ @@ -211,7 +212,7 @@ public final class FormRecognizerAsyncClient { * * @return A {@link PollerFlux} that polls the recognize custom form operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl} is {@code null}. */ @@ -235,7 +236,7 @@ public PollerFlux> beginRecognizeContentFromUrl( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl} is {@code null}. */ @@ -269,7 +270,7 @@ public PollerFlux> beginRecognizeContentFromUrl( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form} is {@code null}. */ @@ -299,7 +300,7 @@ public PollerFlux> beginRecognizeContent( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form} is {@code null}. */ @@ -328,7 +329,7 @@ public PollerFlux> beginRecognizeContent( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @@ -354,7 +355,7 @@ public PollerFlux> beginRecognizeContent( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @@ -388,7 +389,7 @@ public PollerFlux> beginRecognizeContent( * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receipt} is {@code null}. */ @@ -419,7 +420,7 @@ public PollerFlux> beginRecognizeReceip * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receipt} is {@code null}. */ @@ -595,7 +596,7 @@ private Function, Mono> content } /** - * Helper method that throws a {@link HttpResponseException} if {@link AnalyzeOperationResult#getStatus()} is + * Helper method that throws a {@link FormRecognizerException} if {@link AnalyzeOperationResult#getStatus()} is * {@link OperationStatus#FAILED}. * * @param analyzeResponse The response returned from the service. @@ -604,7 +605,7 @@ private void throwIfAnalyzeStatusInvalid(AnalyzeOperationResult analyzeResponse) if (OperationStatus.FAILED.equals(analyzeResponse.getStatus())) { List errorInformationList = analyzeResponse.getAnalyzeResult().getErrors(); if (!CoreUtils.isNullOrEmpty(errorInformationList)) { - throw logger.logExceptionAsError(new HttpResponseException("Analyze operation failed.", null, + throw logger.logExceptionAsError(new FormRecognizerException("Analyze operation failed", errorInformationList)); } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java index 0700a8e22249..ce693dd17ced 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java @@ -8,13 +8,13 @@ import com.azure.ai.formrecognizer.implementation.models.OperationStatus; import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.FormPage; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.core.annotation.ReturnType; import com.azure.core.annotation.ServiceClient; import com.azure.core.annotation.ServiceMethod; -import com.azure.core.exception.HttpResponseException; import com.azure.core.util.polling.SyncPoller; import reactor.core.publisher.Flux; @@ -61,7 +61,7 @@ public final class FormRecognizerClient { * * @return A {@link SyncPoller} to poll the progress of the recognize custom form operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl}, {@code modelId} is {@code null}. */ @@ -88,7 +88,7 @@ public final class FormRecognizerClient { * * @return A {@link SyncPoller} to poll the progress of the recognize custom form operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl}, {@code modelId} is {@code null}. */ @@ -116,7 +116,7 @@ public final class FormRecognizerClient { * * @return A {@link SyncPoller} that polls the recognize custom form operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form}, {@code modelId} is {@code null}. */ @@ -145,7 +145,7 @@ public final class FormRecognizerClient { * * @return A {@link SyncPoller} that polls the recognize custom form operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form}, {@code modelId} is {@code null}. */ @@ -171,7 +171,7 @@ public final class FormRecognizerClient { * * @return A {@link SyncPoller} that polls the recognize layout form operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl} is {@code null}. */ @@ -194,7 +194,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} that polls the recognize layout operation until it has completed, has * failed, or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code formUrl} is {@code null}. */ @@ -218,7 +218,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} that polls the recognize layout operation until it has completed, has failed, or has * been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form} is {@code null}. */ @@ -245,7 +245,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} that polls the recognize layout operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link FormPage}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code form} is {@code null}. */ @@ -270,7 +270,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} to poll the progress of the recognize receipt operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @@ -296,7 +296,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} to poll the progress of the recognize receipt operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @@ -321,7 +321,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} that polls the recognize receipt operation until it has completed, * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receipt} is {@code null}. */ @@ -349,7 +349,7 @@ public SyncPoller> beginRecognizeContentFromUrl( * * @return A {@link SyncPoller} that polls the recognize receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. - * @throws HttpResponseException If recognize operation fails and the {@link AnalyzeOperationResult} returned with + * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receipt} is {@code null}. */ diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/ErrorInformation.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/ErrorInformation.java index 348f1e2927b5..8d61aab94801 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/ErrorInformation.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/ErrorInformation.java @@ -7,9 +7,13 @@ import com.azure.core.annotation.Fluent; import com.fasterxml.jackson.annotation.JsonProperty; +import java.io.Serializable; + /** The ErrorInformation model. */ @Fluent -public final class ErrorInformation { +public final class ErrorInformation implements Serializable { + private static final long serialVersionUID = -5217525992938366429L; + /* * The code property. */ diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormRecognizerException.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormRecognizerException.java new file mode 100644 index 000000000000..e61284c45de5 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormRecognizerException.java @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.formrecognizer.models; + +import com.azure.core.exception.AzureException; + +import java.util.List; + +/** + * General exception for FormRecognizer client-side related failures. + * + * @see ErrorInformation + */ +public class FormRecognizerException extends AzureException { + private final List errorInformationList; + private final String errorInformationMessage; + + /** + * Initializes a new instance of {@link FormRecognizerException} class + * + * @param message Text containing the details of the exception. + * @param errorInformationList The List of error information that caused the exception + */ + public FormRecognizerException(final String message, final List errorInformationList) { + super(message); + StringBuilder errorInformationStringBuilder = new StringBuilder().append(message); + if (errorInformationList.size() > 0) { + for (ErrorInformation errorInformation : errorInformationList) { + errorInformationStringBuilder.append(", " + "errorCode" + ": [" + errorInformation.getCode() + + "], " + "message" + ": " + errorInformation.getMessage()); + } + } + this.errorInformationMessage = errorInformationStringBuilder.toString(); + this.errorInformationList = errorInformationList; + } + + @Override + public String getMessage() { + return this.errorInformationMessage; + } + + /** + * Get the error information list for this exception. + * + * @return the error information list for this exception. + */ + public List getErrorInformation() { + return this.errorInformationList; + } +} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingAsyncClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingAsyncClient.java index 5c7099365be1..99e95d63c2ac 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingAsyncClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingAsyncClient.java @@ -22,6 +22,7 @@ import com.azure.ai.formrecognizer.models.CustomFormModelInfo; import com.azure.ai.formrecognizer.models.CustomFormModelStatus; import com.azure.ai.formrecognizer.models.ErrorInformation; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.TrainingFileFilter; import com.azure.core.annotation.ReturnType; @@ -133,7 +134,7 @@ String getEndpoint() { * * @return A {@link PollerFlux} that polls the training model operation until it has completed, has failed, or has * been cancelled. The completed operation returns a {@link CustomFormModel}. - * @throws HttpResponseException If training fails and model with {@link ModelStatus#INVALID} is created. + * @throws FormRecognizerException If training fails and model with {@link ModelStatus#INVALID} is created. * @throws NullPointerException If {@code trainingFilesUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) @@ -162,7 +163,7 @@ public PollerFlux beginTraining(String trainin * * @return A {@link PollerFlux} that polls the extract receipt operation until it * has completed, has failed, or has been cancelled. The completed operation returns a {@link CustomFormModel}. - * @throws HttpResponseException If training fails and model with {@link ModelStatus#INVALID} is created. + * @throws FormRecognizerException If training fails and model with {@link ModelStatus#INVALID} is created. * @throws NullPointerException If {@code trainingFilesUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) @@ -351,7 +352,9 @@ PagedFlux listCustomModels(Context context) { * generated from the target resource's call to {@link FormTrainingAsyncClient#getCopyAuthorization(String, String)} * * @return A {@link PollerFlux} that polls the copy model operation until it has completed, has failed, - * or has been cancelled. + * or has been cancelled. The completed operation returns the copied model {@link CustomFormModelInfo}. + * @throws FormRecognizerException If copy operation fails and model with {@link OperationStatus#FAILED} is created. + * @throws NullPointerException If {@code modelId}, {@code target} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) public PollerFlux beginCopyModel(String modelId, @@ -380,7 +383,10 @@ public PollerFlux beginCopyModel(String mo * 5 seconds is used. * * @return A {@link PollerFlux} that polls the copy model operation until it has completed, has failed, - * or has been cancelled. + * or has been cancelled. The completed operation returns the copied model {@link CustomFormModelInfo}. + * @throws FormRecognizerException If copy operation fails and model with {@link OperationStatus#FAILED} + * is created. + * @throws NullPointerException If {@code modelId}, {@code target} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) public PollerFlux beginCopyModel(String modelId, @@ -405,6 +411,7 @@ public PollerFlux beginCopyModel(String mo * {@codesnippet com.azure.ai.formrecognizer.training.FormTrainingAsyncClient.getCopyAuthorization#string-string} * * @return The {@link CopyAuthorization} that could be used to authorize copying model between resources. + * @throws NullPointerException If {@code resourceId}, {@code resourceRegion} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) public Mono getCopyAuthorization(String resourceId, String resourceRegion) { @@ -425,6 +432,7 @@ public Mono getCopyAuthorization(String resourceId, String re * * @return A {@link Response} containing the {@link CopyAuthorization} that could be used to authorize copying * model between resources. + * @throws NullPointerException If {@code resourceId}, {@code resourceRegion} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getCopyAuthorizationWithResponse(String resourceId, @@ -635,7 +643,7 @@ private static Mono> processTrainingModelResponse( } /** - * Helper method that throws a {@link HttpResponseException} if {@link CopyOperationResult#getStatus()} is + * Helper method that throws a {@link FormRecognizerException} if {@link CopyOperationResult#getStatus()} is * {@link OperationStatus#FAILED}. * * @param copyResult The copy operation response returned from the service. @@ -644,14 +652,14 @@ private void throwIfCopyOperationStatusInvalid(CopyOperationResult copyResult) { if (copyResult.getStatus().equals(OperationStatus.FAILED)) { List errorInformationList = copyResult.getCopyResult().getErrors(); if (!CoreUtils.isNullOrEmpty(errorInformationList)) { - throw logger.logExceptionAsError(new HttpResponseException("Copy operation returned with a failed " - + "status", null, errorInformationList)); + throw logger.logExceptionAsError(new FormRecognizerException("Copy operation returned with a failed " + + "status", errorInformationList)); } } } /** - * Helper method that throws a {@link HttpResponseException} if {@link ModelInfo#getStatus()} is + * Helper method that throws a {@link FormRecognizerException} if {@link ModelInfo#getStatus()} is * {@link com.azure.ai.formrecognizer.implementation.models.ModelStatus#INVALID}. * * @param customModel The response returned from the service. @@ -660,9 +668,9 @@ private void throwIfModelStatusInvalid(Model customModel) { if (ModelStatus.INVALID.equals(customModel.getModelInfo().getStatus())) { List errorInformationList = customModel.getTrainResult().getErrors(); if (!CoreUtils.isNullOrEmpty(errorInformationList)) { - throw logger.logExceptionAsError(new HttpResponseException( + throw logger.logExceptionAsError(new FormRecognizerException( String.format("Invalid model created with ID: %s", customModel.getModelInfo().getModelId()), - null, errorInformationList)); + errorInformationList)); } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingClient.java index 321caea7b987..2fae379146ac 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/training/FormTrainingClient.java @@ -10,12 +10,12 @@ import com.azure.ai.formrecognizer.models.CopyAuthorization; import com.azure.ai.formrecognizer.models.CustomFormModel; import com.azure.ai.formrecognizer.models.CustomFormModelInfo; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.TrainingFileFilter; import com.azure.core.annotation.ReturnType; import com.azure.core.annotation.ServiceClient; import com.azure.core.annotation.ServiceMethod; -import com.azure.core.exception.HttpResponseException; import com.azure.core.http.rest.PagedIterable; import com.azure.core.http.rest.Response; import com.azure.core.util.Context; @@ -79,7 +79,7 @@ public FormRecognizerClient getFormRecognizerClient() { * * @return A {@link SyncPoller} that polls the training model operation until it has completed, has failed, or has * been cancelled. The completed operation returns a {@link CustomFormModel}. - * @throws HttpResponseException If training fails and model with {@link ModelStatus#INVALID} is created. + * @throws FormRecognizerException If training fails and model with {@link ModelStatus#INVALID} is created. * @throws NullPointerException If {@code trainingFilesUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) @@ -108,7 +108,7 @@ public SyncPoller beginTraining(String trainin * * @return A {@link SyncPoller} that polls the extract receipt operation until it has completed, has failed, * or has been cancelled. The completed operation returns a {@link CustomFormModel}. - * @throws HttpResponseException If training fails and model with {@link ModelStatus#INVALID} is created. + * @throws FormRecognizerException If training fails and model with {@link ModelStatus#INVALID} is created. * @throws NullPointerException If {@code trainingFilesUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java index 7cdcffd6b094..546534eacf20 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java @@ -8,11 +8,11 @@ import com.azure.ai.formrecognizer.models.ErrorResponseException; import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.FormPage; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingAsyncClient; -import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; import com.azure.core.util.polling.SyncPoller; import org.junit.jupiter.api.AfterAll; @@ -445,12 +445,12 @@ void recognizeCustomFormInvalidStatus(HttpClient httpClient, FormRecognizerServi getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(training, useTrainingLabels).getSyncPoller(); syncPoller.waitForCompletion(); CustomFormModel createdModel = syncPoller.getFinalResult(); - HttpResponseException httpResponseException = assertThrows(HttpResponseException.class, + FormRecognizerException formRecognizerException = assertThrows(FormRecognizerException.class, () -> client.beginRecognizeCustomFormsFromUrl(invalidSourceUrl, createdModel.getModelId()).getSyncPoller().getFinalResult()); - ErrorInformation errorInformation = (ErrorInformation) ((List) httpResponseException.getValue()).get(0); - assertEquals("Analyze operation failed.", httpResponseException.getMessage()); + ErrorInformation errorInformation = formRecognizerException.getErrorInformation().get(0); assertEquals(EXPECTED_INVALID_URL_ERROR_CODE, errorInformation.getCode()); assertEquals(OCR_EXTRACTION_INVALID_URL_ERROR, errorInformation.getMessage()); + assertEquals(EXPECTED_INVALID_ANALYZE_EXCEPTION_MESSAGE, formRecognizerException.getMessage()); })); } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java index 3f29134d4f98..978aa2a33f5a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java @@ -8,11 +8,11 @@ import com.azure.ai.formrecognizer.models.ErrorResponseException; import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.FormPage; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingClient; -import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; import com.azure.core.util.polling.SyncPoller; import org.junit.jupiter.params.ParameterizedTest; @@ -409,11 +409,13 @@ void recognizeCustomFormInvalidStatus(HttpClient httpClient, FormRecognizerServi getFormTrainingClient(httpClient, serviceVersion).beginTraining(training, useTrainingLabels); syncPoller.waitForCompletion(); CustomFormModel createdModel = syncPoller.getFinalResult(); - HttpResponseException httpResponseException = assertThrows(HttpResponseException.class, - () -> client.beginRecognizeCustomFormsFromUrl(invalidSourceUrl, createdModel.getModelId()).getFinalResult()); - ErrorInformation errorInformation = (ErrorInformation) ((List) httpResponseException.getValue()).get(0); + FormRecognizerException formRecognizerException = assertThrows(FormRecognizerException.class, + () -> client.beginRecognizeCustomFormsFromUrl(invalidSourceUrl, createdModel.getModelId()) + .getFinalResult()); + ErrorInformation errorInformation = formRecognizerException.getErrorInformation().get(0); assertEquals(EXPECTED_INVALID_URL_ERROR_CODE, errorInformation.getCode()); assertEquals(OCR_EXTRACTION_INVALID_URL_ERROR, errorInformation.getMessage()); + assertEquals(EXPECTED_INVALID_ANALYZE_EXCEPTION_MESSAGE, formRecognizerException.getMessage()); }); }); } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java index f89c2a534591..096dc1ebc961 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java @@ -74,8 +74,12 @@ public abstract class FormRecognizerClientTestBase extends TestBase { private static final String EXPECTED_MULTIPAGE_ADDRESS_VALUE = "123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA"; private static final String EXPECTED_MULTIPAGE_PHONE_NUMBER_VALUE = "+15555555555"; private static final String ITEMIZED_RECEIPT_VALUE = "Itemized"; - static final String OCR_EXTRACTION_INVALID_URL_ERROR = "OCR extraction error: [Wrong response code: InvalidImageURL. Message: Image URL is badly formatted..]"; + static final String OCR_EXTRACTION_INVALID_URL_ERROR = "OCR extraction error: [Wrong response code: " + + "InvalidImageURL. Message: Image URL is badly formatted..]"; static final String EXPECTED_INVALID_URL_ERROR_CODE = "3003"; + static final String EXPECTED_INVALID_ANALYZE_EXCEPTION_MESSAGE = + "Analyze operation failed, " + "errorCode: [" + EXPECTED_INVALID_URL_ERROR_CODE + "], " + + "message: " + OCR_EXTRACTION_INVALID_URL_ERROR; FormRecognizerClientBuilder getFormRecognizerClientBuilder(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java index 24ccd9dc68ba..d5895760312a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java @@ -5,9 +5,9 @@ import com.azure.ai.formrecognizer.models.CustomFormModel; import com.azure.ai.formrecognizer.models.ErrorInformation; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.training.FormTrainingAsyncClient; -import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; import com.azure.core.util.polling.SyncPoller; import io.netty.handler.codec.http.HttpResponseStatus; @@ -18,7 +18,6 @@ import reactor.test.StepVerifier; import java.time.Duration; -import java.util.List; import static com.azure.ai.formrecognizer.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS; import static com.azure.ai.formrecognizer.TestUtils.INVALID_MODEL_ID; @@ -338,19 +337,19 @@ public void beginTrainingUnlabeledResult(HttpClient httpClient, FormRecognizerSe // } /** - * Verifies the training operation throws HttpResponseException when an invalid status model is returned. + * Verifies the training operation throws FormRecognizerException when an invalid status model is returned. */ @ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS) @MethodSource("com.azure.ai.formrecognizer.TestUtils#getTestParameters") void beginTrainingInvalidModelStatus(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormTrainingAsyncClient(httpClient, serviceVersion); beginTrainingInvalidModelStatusRunner((invalidTrainingFilesUrl, useTrainingLabels) -> { - HttpResponseException httpResponseException = assertThrows(HttpResponseException.class, + FormRecognizerException formRecognizerException = assertThrows(FormRecognizerException.class, () -> client.beginTraining(invalidTrainingFilesUrl, useTrainingLabels).getSyncPoller().getFinalResult()); - ErrorInformation errorInformation = (ErrorInformation) ((List) httpResponseException.getValue()).get(0); - assertTrue(httpResponseException.getMessage().contains(EXPECTED_INVALID_MODEL_STATUS_MESSAGE)); + ErrorInformation errorInformation = formRecognizerException.getErrorInformation().get(0); assertEquals(EXPECTED_INVALID_MODEL_STATUS_ERROR_CODE, errorInformation.getCode()); - assertEquals(EXPECTED_INVALID_STATUS_ERROR_INFORMATION, errorInformation.getMessage()); + assertEquals(EXPECTED_INVALID_MODEL_ERROR, errorInformation.getMessage()); + assertTrue(formRecognizerException.getMessage().contains(EXPECTED_INVALID_STATUS_EXCEPTION_MESSAGE)); }); } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java index 05e6bba2f5a9..cf63223f731c 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java @@ -8,9 +8,9 @@ import com.azure.ai.formrecognizer.models.CustomFormModelInfo; import com.azure.ai.formrecognizer.models.ErrorInformation; import com.azure.ai.formrecognizer.models.ErrorResponseException; +import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.training.FormTrainingClient; -import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; import com.azure.core.http.rest.Response; import com.azure.core.util.Context; @@ -19,8 +19,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import java.util.List; - import static com.azure.ai.formrecognizer.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS; import static com.azure.ai.formrecognizer.TestUtils.INVALID_MODEL_ID; import static com.azure.ai.formrecognizer.TestUtils.INVALID_MODEL_ID_ERROR; @@ -291,19 +289,19 @@ public void beginTrainingUnlabeledResult(HttpClient httpClient, FormRecognizerSe // } /** - * Verifies the training operation throws HttpResponseException when an invalid status model is returned. + * Verifies the training operation throws FormRecognizerException when an invalid status model is returned. */ @ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS) @MethodSource("com.azure.ai.formrecognizer.TestUtils#getTestParameters") void beginTrainingInvalidModelStatus(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormTrainingClient(httpClient, serviceVersion); beginTrainingInvalidModelStatusRunner((invalidTrainingFilesUrl, useTrainingLabels) -> { - HttpResponseException httpResponseException = assertThrows(HttpResponseException.class, + FormRecognizerException formRecognizerException = assertThrows(FormRecognizerException.class, () -> client.beginTraining(invalidTrainingFilesUrl, useTrainingLabels).getFinalResult()); - ErrorInformation errorInformation = (ErrorInformation) ((List) httpResponseException.getValue()).get(0); - assertTrue(httpResponseException.getMessage().contains(EXPECTED_INVALID_MODEL_STATUS_MESSAGE)); + ErrorInformation errorInformation = formRecognizerException.getErrorInformation().get(0); assertEquals(EXPECTED_INVALID_MODEL_STATUS_ERROR_CODE, errorInformation.getCode()); - assertEquals(EXPECTED_INVALID_STATUS_ERROR_INFORMATION, errorInformation.getMessage()); + assertEquals(EXPECTED_INVALID_MODEL_ERROR, errorInformation.getMessage()); + assertTrue(formRecognizerException.getMessage().contains(EXPECTED_INVALID_STATUS_EXCEPTION_MESSAGE)); }); } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java index 2807cf4a75c2..e71eae01d700 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java @@ -48,9 +48,11 @@ public abstract class FormTrainingClientTestBase extends TestBase { "FORM_RECOGNIZER_TESTING_BLOB_CONTAINER_SAS_URL"; static final String AZURE_FORM_RECOGNIZER_API_KEY = "AZURE_FORM_RECOGNIZER_API_KEY"; static final String AZURE_FORM_RECOGNIZER_ENDPOINT = "AZURE_FORM_RECOGNIZER_ENDPOINT"; - static final String EXPECTED_INVALID_MODEL_STATUS_MESSAGE = "Invalid model created with ID"; + static final String EXPECTED_INVALID_MODEL_STATUS_MESSAGE = "Invalid model created with ID: cae9d062-71e0-44a3-8630-70b32ae94f4d"; + static final String EXPECTED_INVALID_MODEL_ERROR = "Unable to list blobs on the Azure blob storage account."; static final String EXPECTED_INVALID_MODEL_STATUS_ERROR_CODE = "2012"; - static final String EXPECTED_INVALID_STATUS_ERROR_INFORMATION = "Unable to list blobs on the Azure blob storage account."; + static final String EXPECTED_INVALID_STATUS_EXCEPTION_MESSAGE = ", errorCode: [" + EXPECTED_INVALID_MODEL_STATUS_ERROR_CODE + "], " + + "message: " + EXPECTED_INVALID_MODEL_ERROR; private static final String RESOURCE_ID = "FORM_RECOGNIZER_TARGET_RESOURCE_ID"; private static final String RESOURCE_REGION = "FORM_RECOGNIZER_TARGET_RESOURCE_REGION";