Skip to content

Commit

Permalink
[Text Analytics] Simplifying error handling (Azure#22125)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyaaeldeen authored Jun 7, 2022
1 parent e10172a commit cbe1290
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
31 changes: 10 additions & 21 deletions sdk/textanalytics/ai-text-analytics/src/lro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -75,9 +75,9 @@ async function sendRequest<TOptions extends OperationOptions>(settings: {
httpMethod?: HttpMethods;
}): Promise<LroResponse<unknown>> {
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 },
Expand All @@ -88,12 +88,9 @@ async function sendRequest<TOptions extends OperationOptions>(settings: {
}
),
finalOptions
);
return response;
} catch (e: unknown) {
throw transformError(e);
}
});
)
)
);
}

/**
Expand All @@ -107,7 +104,7 @@ export function createSendPollRequest<TOptions extends OperationOptions>(setting
}): (path: string) => Promise<LroResponse<unknown>> {
const { client, options, tracing, spanStr } = settings;
return async (path: string): Promise<LroResponse<unknown>> => {
return throwTransformErrors(
return throwError(
sendRequest({
client,
opOptions: options,
Expand Down Expand Up @@ -156,7 +153,7 @@ export function createAnalyzeBatchLro(settings: {
...initialRequestOptions,
},
async (finalOptions) =>
throwTransformErrors(
throwError(
getRawResponse(
(paramOptions) =>
client.analyzeText.submitJob(
Expand Down Expand Up @@ -288,14 +285,6 @@ export function createUpdateAnalyzeState(documents?: TextDocumentInput[]) {
};
}

async function throwTransformErrors<T>(p: Promise<T>): Promise<T> {
try {
return await p;
} catch (e: unknown) {
throw transformError(e);
}
}

/**
* @internal
*/
Expand All @@ -307,7 +296,7 @@ export function createCancelOperation(settings: {
return async ({ operationId }): Promise<void> => {
const { client, options, tracing } = settings;
await tracing.withSpan(`${clientName}.beginAnalyzeBatch`, options, async (finalOptions) =>
throwTransformErrors(
throwError(
getRawResponse(
(paramOptions) => client.analyzeText.cancelJob(operationId, paramOptions),
finalOptions
Expand Down
38 changes: 19 additions & 19 deletions sdk/textanalytics/ai-text-analytics/src/textAnalysisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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<ActionName>;
} 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<ActionName>
)
)
);
}

Expand Down
10 changes: 9 additions & 1 deletion sdk/textanalytics/ai-text-analytics/src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -319,6 +319,14 @@ export function transformError(errorResponse: unknown): any {
});
}

export async function throwError<T>(p: Promise<T>): Promise<T> {
try {
return await p;
} catch (e: unknown) {
throw transformError(e);
}
}

function toHealthcareResult(
documents: TextDocumentInput[],
results: GeneratedHealthcareResult
Expand Down

0 comments on commit cbe1290

Please sign in to comment.