Skip to content

Commit

Permalink
fix: handle http error that has no response (#10250)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackchoey authored Oct 31, 2023
1 parent 4e2e536 commit c54ce47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/fx-core/src/component/driver/aad/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ export class UpdateAadAppDriver implements StepDriver {
summaries: summaries,
};
}
if (axios.isAxiosError(error)) {
const message = JSON.stringify(error.response!.data);
if (
axios.isAxiosError(error) &&
error.response // If no response, treat as unhandled error first to understand the actual problem
) {
const message = JSON.stringify(error.response.data);
context.logProvider?.error(
getLocalizedString(logMessageKeys.failExecuteDriver, actionName, message)
);
if (error.response!.status >= 400 && error.response!.status < 500) {
if (error.response.status >= 400 && error.response.status < 500) {
return {
result: err(new HttpClientError(error, actionName, message, helpLink)),
summaries: summaries,
Expand Down
26 changes: 26 additions & 0 deletions packages/fx-core/tests/component/driver/aad/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
InvalidActionInputError,
JSONSyntaxError,
MissingEnvironmentVariablesError,
UnhandledError,
} from "../../../../src/error/common";
import { Platform, ok, err } from "@microsoft/teamsfx-api";
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -478,6 +479,31 @@ describe("aadAppUpdate", async () => {
);
});

it("should throw unhandled error when axios error contains no response", async () => {
sinon.stub(AadAppClient.prototype, "updateAadApp").rejects({
isAxiosError: true,
});
envRestore = mockedEnv({
AAD_APP_OBJECT_ID: expectedObjectId,
AAD_APP_CLIENT_ID: expectedClientId,
});

const args = {
manifestPath: path.join(testAssetsRoot, "manifest.json"),
outputFilePath: path.join(outputRoot, "manifest.output.json"),
};

const result = await updateAadAppDriver.execute(args, mockedDriverContext);

expect(result.result.isErr()).to.be.true;
expect(result.result._unsafeUnwrapErr())
.is.instanceOf(UnhandledError)
.and.property("message")
.equals(
'An unexpected error has occurred while performing the aadApp/update task. {"isAxiosError":true}'
);
});

it("should send telemetries when success", async () => {
const mockedTelemetryReporter = new MockedTelemetryReporter();
let startTelemetry: any, endTelemetry: any;
Expand Down

0 comments on commit c54ce47

Please sign in to comment.