diff --git a/sdk/textanalytics/ai-text-analytics/src/lro.ts b/sdk/textanalytics/ai-text-analytics/src/lro.ts index b72f8ac3cf72..186ef42b42a0 100644 --- a/sdk/textanalytics/ai-text-analytics/src/lro.ts +++ b/sdk/textanalytics/ai-text-analytics/src/lro.ts @@ -19,7 +19,7 @@ import { } from "@azure/core-client"; import { LongRunningOperation, LroResponse, RawResponse } from "@azure/core-lro"; import { PagedResult, getPagedAsyncIterator } from "@azure/core-paging"; -import { transformAnalyzeBatchResults, transformError } from "./transforms"; +import { throwError, transformAnalyzeBatchResults } from "./transforms"; import { HttpMethods } from "@azure/core-rest-pipeline"; import { TracingClient } from "@azure/core-tracing"; import { clientName } from "./constants"; @@ -75,9 +75,9 @@ async function sendRequest(settings: { httpMethod?: HttpMethods; }): Promise> { const { client, opOptions, path, spanStr, spec, tracing, httpMethod = "GET" } = settings; - return tracing.withSpan(spanStr, opOptions, async (finalOptions: TOptions) => { - try { - const response = getRawResponse( + return tracing.withSpan(spanStr, opOptions, async (finalOptions: TOptions) => + throwError( + getRawResponse( (options) => client.sendOperationRequest( { options }, @@ -88,12 +88,9 @@ async function sendRequest(settings: { } ), finalOptions - ); - return response; - } catch (e: unknown) { - throw transformError(e); - } - }); + ) + ) + ); } /** @@ -107,7 +104,7 @@ export function createSendPollRequest(setting }): (path: string) => Promise> { const { client, options, tracing, spanStr } = settings; return async (path: string): Promise> => { - return throwTransformErrors( + return throwError( sendRequest({ client, opOptions: options, @@ -156,7 +153,7 @@ export function createAnalyzeBatchLro(settings: { ...initialRequestOptions, }, async (finalOptions) => - throwTransformErrors( + throwError( getRawResponse( (paramOptions) => client.analyzeText.submitJob( @@ -288,14 +285,6 @@ export function createUpdateAnalyzeState(documents?: TextDocumentInput[]) { }; } -async function throwTransformErrors(p: Promise): Promise { - try { - return await p; - } catch (e: unknown) { - throw transformError(e); - } -} - /** * @internal */ @@ -307,7 +296,7 @@ export function createCancelOperation(settings: { return async ({ operationId }): Promise => { const { client, options, tracing } = settings; await tracing.withSpan(`${clientName}.beginAnalyzeBatch`, options, async (finalOptions) => - throwTransformErrors( + throwError( getRawResponse( (paramOptions) => client.analyzeText.cancelJob(operationId, paramOptions), finalOptions diff --git a/sdk/textanalytics/ai-text-analytics/src/textAnalysisClient.ts b/sdk/textanalytics/ai-text-analytics/src/textAnalysisClient.ts index 40af6013452e..a1dc1dc8f302 100644 --- a/sdk/textanalytics/ai-text-analytics/src/textAnalysisClient.ts +++ b/sdk/textanalytics/ai-text-analytics/src/textAnalysisClient.ts @@ -22,7 +22,6 @@ import { import { DEFAULT_COGNITIVE_SCOPE, SDK_VERSION } from "./constants"; import { InternalPipelineOptions, - RestError, bearerTokenAuthenticationPolicy, } from "@azure/core-rest-pipeline"; import { KeyCredential, TokenCredential, isTokenCredential } from "@azure/core-auth"; @@ -42,7 +41,7 @@ import { getDocsFromState, processAnalyzeResult, } from "./lro"; -import { transformActionResult, transformError } from "./transforms"; +import { throwError, transformActionResult } from "./transforms"; import { GeneratedClient } from "./generated/generatedClient"; import { logger } from "./logger"; import { textAnalyticsAzureKeyCredentialPolicy } from "./azureKeyCredentialPolicy"; @@ -495,23 +494,24 @@ export class TextAnalysisClient { return this._tracing.withSpan( "TextAnalysisClient.analyze", operationOptions, - async (updatedOptions: TextAnalysisOperationOptions) => { - try { - const result = await this._client.analyze( - { - kind: actionName, - analysisInput: { - documents: realInputs, - }, - parameters: action, - } as any, - updatedOptions - ); - return transformActionResult(actionName, realInputs, result) as AnalyzeResult; - } catch (e: unknown) { - throw transformError(e as RestError); - } - } + async (updatedOptions: TextAnalysisOperationOptions) => + throwError( + this._client + .analyze( + { + kind: actionName, + analysisInput: { + documents: realInputs, + }, + parameters: action, + } as any, + updatedOptions + ) + .then( + (result) => + transformActionResult(actionName, realInputs, result) as AnalyzeResult + ) + ) ); } diff --git a/sdk/textanalytics/ai-text-analytics/src/transforms.ts b/sdk/textanalytics/ai-text-analytics/src/transforms.ts index f12904e6ab39..b4136784ff35 100644 --- a/sdk/textanalytics/ai-text-analytics/src/transforms.ts +++ b/sdk/textanalytics/ai-text-analytics/src/transforms.ts @@ -285,7 +285,7 @@ function appendReadableErrorMessage(currentMessage: string, innerMessage: string * parses incoming errors from the service/ * @param error - the incoming error */ -export function transformError(errorResponse: unknown): any { +function transformError(errorResponse: unknown): any { const strongErrorResponse = errorResponse as { response: { parsedBody?: ErrorResponse; @@ -319,6 +319,14 @@ export function transformError(errorResponse: unknown): any { }); } +export async function throwError(p: Promise): Promise { + try { + return await p; + } catch (e: unknown) { + throw transformError(e); + } +} + function toHealthcareResult( documents: TextDocumentInput[], results: GeneratedHealthcareResult