Skip to content

Commit

Permalink
chore: fix not calling enable skill when get enablement returned 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Doiron committed May 10, 2023
1 parent fd20898 commit 02c3a9f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
16 changes: 13 additions & 3 deletions lib/clients/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ export function request(options, operation, doDebug, callback) {
loggerInstance().debug(debugContentForResponse(operation, null, response));
}
if (!response) {
return callback(`The request to ${operation}, failed.\nPlease make sure "${requestOptions.url}" is responding.`);
return callback({
errorMessage :`The request to ${operation}, failed.\nPlease make sure "${requestOptions.url}" is responding.`,
});
}
if (!response.status) {
return callback(`Failed to access the statusCode from the request to ${operation}.`);
return callback({
errorMessage :`Failed to access the statusCode from the request to ${operation}.`,
});
}
return callback(null, {
statusCode: response.status,
Expand All @@ -74,7 +78,13 @@ export function request(options, operation, doDebug, callback) {
if (doDebug) {
loggerInstance().debug(debugContentForResponse(operation, error, response));
}
return callback(`The request to ${requestOptions.url} failed. Client Error: ${error}`, response);

return callback({
errorMessage : `The request to ${requestOptions.url} failed. Client Error: ${error}`,
statusCode: response.status,
...(response.data ? {body: response.data} : {}),
...(response.headers ? {headers: response.headers} : {}),
}, response);
});
}
/**
Expand Down
22 changes: 11 additions & 11 deletions lib/clients/lwa-auth-code-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ module.exports = class LWAAuthCodeClient {
body,
json: !!body,
};
httpClient.request(options, "GET_ACCESS_TOKEN", this.config.doDebug, (err, response) => {
if (err) {
return callback(err);
httpClient.request(options, "GET_ACCESS_TOKEN", this.config.doDebug, (requestError, requestResponse) => {
if (requestError) {
return callback(requestError.errorMessage || requestError);
}
const tokenBody = R.clone(response.body);
const tokenBody = R.clone(requestResponse.body);
if (tokenBody.error) {
return callback(new CliError(tokenBody.error));
}
Expand Down Expand Up @@ -69,20 +69,20 @@ module.exports = class LWAAuthCodeClient {
},
json: true,
};
httpClient.request(options, "GET_ACCESS_TOKEN_USING_REFRESH_TOKEN", this.config.doDebug, (err, response) => {
if (err) {
return callback(err);
httpClient.request(options, "GET_ACCESS_TOKEN_USING_REFRESH_TOKEN", this.config.doDebug, (requestError, requestResponse) => {
if (requestError) {
return callback(requestError.message || requestError);
}
const responseErr = R.view(R.lensPath(["body", "error"]), response);
const responseErr = R.view(R.lensPath(["body", "error"]), requestResponse);
if (stringUtils.isNonBlankString(responseErr)) {
return callback(`Refresh LWA tokens failed, please run "ask configure" to manually update your tokens. Error: ${responseErr}.`);
}
const expiresIn = R.view(R.lensPath(["body", "expires_in"]), response);
const expiresIn = R.view(R.lensPath(["body", "expires_in"]), requestResponse);
if (!expiresIn) {
return callback(`Received invalid response body from LWA without "expires_in":\n${jsonView.toString(response.body)}`);
return callback(`Received invalid response body from LWA without "expires_in":\n${jsonView.toString(requestResponse.body)}`);
}

const tokenBody = R.clone(response.body);
const tokenBody = R.clone(requestResponse.body);
if (tokenBody.error) {
return callback(new CliError(tokenBody.error));
}
Expand Down
14 changes: 7 additions & 7 deletions lib/clients/smapi-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,17 @@ export class SmapiClientLateBound {
body: payload,
json: !!payload,
};
httpClient.request(requestOptions, apiName, configuration.doDebug, (reqErr: any, reqResponse: SmapiResponse<T>) => {
if (reqErr && reqErr.statusCode ) {
return _normalizeSmapiResponse<T>(reqErr, (normalizeErr, smapiResponse) => {
httpClient.request(requestOptions, apiName, configuration.doDebug, (requestError: any, requestResponse: SmapiResponse<T>) => {
if (requestError && requestError.statusCode ) {
return _normalizeSmapiResponse<T>(requestError, (normalizeErr, smapiResponse) => {
return callback(normalizeErr || smapiResponse, smapiResponse || null);
});
}
if (reqErr) {
return callback(reqErr);
if (requestError) {
return callback(requestError.errorMessage || requestError);
}
return _normalizeSmapiResponse<T>(reqResponse, (normalizeErr, smapiResponse) => {
return callback(normalizeErr, normalizeErr ? null : smapiResponse);
return _normalizeSmapiResponse<T>(requestResponse, (normalizeError, smapiResponse) => {
return callback(normalizeError, normalizeError ? null : smapiResponse);
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions lib/clients/smapi-client/smapiApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ export class SmapiApiClient implements ApiClient {
body: data,
};

httpClient.request(requestOptions, "SMAPI_API_CLIENT", this.doDebug, (reqErr: any, reqResponse: SmapiResponse<any>) => {
if (reqErr) {
return reject(new Error(reqErr));
httpClient.request(requestOptions, "SMAPI_API_CLIENT", this.doDebug, (requestError: any, requestResponse: SmapiResponse<any>) => {
if (requestError) {
return reject(new Error(requestError.errorMessage || requestError));
}
const dataContent = reqResponse?.body || {};
const dataContent = requestResponse?.body || {};

resolve({
statusCode: reqResponse?.statusCode!,
statusCode: requestResponse?.statusCode!,
body: this.sanitizeDataContent(dataContent),
headers: reqResponse?.headers!,
headers: requestResponse?.headers!,
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/skill/add-locales/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function _getNewLocaleModelUri(selectedLocales, profile, doDebug, callback) {
doDebug,
(error, templateMapResponse) => {
if (error) {
return callback(`Failed to retrieve the interaction model map template list.\nError: ${JSON.stringify(error, null, 2)}`);
return callback(`Failed to retrieve the interaction model map template list.\nError: ${JSON.stringify(error.errorMessage || error, null, 2)}`);
}
const templateIndexMap = templateMapResponse.body;
// assign for each locale
Expand Down Expand Up @@ -158,10 +158,10 @@ function _retrieveTemplatesByLanguage(templateSet, doDebug, callback) {
(error, response) => {
if (error) {
if (error.statusCode > 300) {
return loopCallback(`Failed to retrieve the template list, please see the details from the error response.\n${JSON.stringify(error, null, 2)}`);
return loopCallback(`Failed to retrieve the template list, please see the details from the error response.\n${JSON.stringify(error.errorMessage || error, null, 2)}`);
}

return loopCallback(`Failed to retrieve the template list.\n${error}`);
return loopCallback(`Failed to retrieve the template list.\n${error.errorMessage || error}`);
}

result.set(templateUrl, response.body);
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/zip-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ function unzipRemoteZipFile(url, targetPath, doDebug, callback) {
},
"get-zip-file",
doDebug,
(err, response) => {
if (err) {
return callback(err);
(requestError, requestResponse) => {
if (requestError) {
return callback(requestError.errorMessage || requestError);
}
const zip = new AdmZip(response.body);
const zip = new AdmZip(requestResponse.body);
try {
zip.extractAllTo(targetPath, false);
} catch (unzipErr) {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/clients/http-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("Clients test - cli http request client", () => {
// call
httpClient.request(VALID_OPTIONS, VALID_OPERATION, false, (err, response) => {
// verify
expect(err).equal(`The request to ${VALID_OPTIONS.url} failed. Client Error: error`);
expect(err.errorMessage).equal(`The request to ${VALID_OPTIONS.url} failed. Client Error: error`);
expect(response).deep.equal({});
done();
});
Expand All @@ -201,7 +201,7 @@ describe("Clients test - cli http request client", () => {
// call
httpClient.request(VALID_OPTIONS, VALID_OPERATION, false, (err, response) => {
// verify
expect(err).equal(`The request to ${VALID_OPERATION}, failed.\nPlease make sure "${VALID_OPTIONS.url}" is responding.`);
expect(err.errorMessage).equal(`The request to ${VALID_OPERATION}, failed.\nPlease make sure "${VALID_OPTIONS.url}" is responding.`);
expect(response).equal(undefined);
done();
});
Expand All @@ -213,7 +213,7 @@ describe("Clients test - cli http request client", () => {
// call
httpClient.request(VALID_OPTIONS, VALID_OPERATION, false, (err, response) => {
// verify
expect(err).equal(`Failed to access the statusCode from the request to ${VALID_OPERATION}.`);
expect(err.errorMessage).equal(`Failed to access the statusCode from the request to ${VALID_OPERATION}.`);
expect(response).equal(undefined);
done();
});
Expand Down Expand Up @@ -286,7 +286,7 @@ describe("Clients test - cli http request client", () => {
expect(stubRequest.args[0][0].method).equal(HTTP_REQUEST.VERB.PUT);
expect(stubRequest.args[0][0].data).equal(TEST_PAYLOAD);
expect(res).deep.equal({});
expect(err).equal(`The request to ${TEST_UPLOAD_URL} failed. Client Error: uploadErr`);
expect(err.errorMessage).equal(`The request to ${TEST_UPLOAD_URL} failed. Client Error: uploadErr`);
done();
});
});
Expand Down

0 comments on commit 02c3a9f

Please sign in to comment.