Skip to content

Commit

Permalink
fix: Print error message for invoke command (#458)
Browse files Browse the repository at this point in the history
* fix: Print error message for invoke command

* feat: Add version to user agent

* fix lint errors

* fix tests
  • Loading branch information
tbarlow12 committed May 19, 2020
1 parent 0117ce3 commit 947a305
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/services/functionAppService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe("Function App Service", () => {
Authorization: `Bearer ${(await variables["azureCredentials"].getToken()).accessToken}`,
Accept: "*/*",
ContentType: "application/octet-stream",
"User-Agent": "serverless_framework",
"User-Agent": expect.stringContaining("serverless_framework"),
}
}, defaultArtifact);
const expectedArtifactName = service.getDeploymentName().replace("rg-deployment", "artifact");
Expand Down Expand Up @@ -331,7 +331,7 @@ describe("Function App Service", () => {
Authorization: `Bearer ${(await variables["azureCredentials"].getToken()).accessToken}`,
Accept: "*/*",
ContentType: "application/octet-stream",
"User-Agent": "serverless_framework",
"User-Agent": expect.stringContaining("serverless_framework"),
}
}, "app.zip");
expectLogFunctions(sls);
Expand Down Expand Up @@ -359,7 +359,7 @@ describe("Function App Service", () => {
Authorization: `Bearer ${(await variables["azureCredentials"].getToken()).accessToken}`,
Accept: "*/*",
ContentType: "application/octet-stream",
"User-Agent": "serverless_framework",
"User-Agent": expect.stringContaining("serverless_framework"),
}
}, slsService["artifact"]);
});
Expand Down
5 changes: 4 additions & 1 deletion src/services/functionAppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AzureBlobStorageService } from "./azureBlobStorageService";
import { BaseService } from "./baseService";
import { constants } from "../shared/constants";
import { FunctionAppOS } from "../config/runtime";
const packageJson = require("../../package.json"); // eslint-disable-line @typescript-eslint/no-var-requires

export class FunctionAppService extends BaseService {
private static readonly retryCount: number = 30;
Expand Down Expand Up @@ -220,10 +221,12 @@ export class FunctionAppService extends BaseService {
Authorization: `Bearer ${await this.getAccessToken()}`,
Accept: "*/*",
ContentType: "application/octet-stream",
"User-Agent": "serverless_framework", //TODO Underscore version name of plugin/cli
"User-Agent": `serverless_framework/${this.serverless.version} serverless_azure_functions/${packageJson.version}`, //TODO Underscore version name of plugin/cli
}
};

this.prettyPrint(requestOptions);

await this.sendFile(requestOptions, functionZipFile);
this.log("-> Function package uploaded successfully");
}
Expand Down
13 changes: 11 additions & 2 deletions src/services/invokeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe("Invoke Service ", () => {
const testData = "test-data";
const testResult = "test result";
const authKey = "authKey";
const errorCode = 400;
const errorData = "This was a bad request";
const baseUrl = "https://management.azure.com"
const masterKeyUrl = `https://${app.defaultHostName}/admin/host/systemkeys/_master`;
const authKeyUrl = `${baseUrl}${app.id}/functions/admin/token?api-version=2016-08-01`;
Expand All @@ -29,15 +31,15 @@ describe("Invoke Service ", () => {

let masterKey: string;
let sls = MockFactory.createTestServerless();
const axiosMock = new MockAdapter(axios);

let options = {
function: functionName,
data: JSON.stringify({name: testData}),
method: "GET"
} as any;

beforeAll(() => {
const axiosMock = new MockAdapter(axios);
beforeEach(() => {
// Master Key
axiosMock.onGet(masterKeyUrl).reply(200, { value: masterKey });
// Auth Key
Expand Down Expand Up @@ -110,4 +112,11 @@ describe("Invoke Service ", () => {
await service.invoke("GET", fakeName);
expect(sls.cli.log).lastCalledWith(`Function ${fakeName} does not exist`)
});

it("throws an error with status code and data", async () => {
// Mock url for POST
axiosMock.onPost(urlPOST).reply(errorCode, errorData);
const service = new InvokeService(sls, options);
await expect(service.invoke("POST", options.function, options.data)).rejects.toThrow(`HTTP Error: ${errorCode} - ${errorData}`);
});
});
12 changes: 9 additions & 3 deletions src/services/invokeService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { AxiosRequestConfig } from "axios";
import { stringify } from "querystring";
import Serverless from "serverless";
import { ApimResource } from "../armTemplates/resources/apim";
Expand Down Expand Up @@ -46,16 +46,22 @@ export class InvokeService extends BaseService {
this.log("Needs to be an http function");
return;
}
const options: AxiosRequestConfig = await this.getRequestOptions(method, data);

let url = await this.getUrl(functionName);
if (method === "GET" && data) {
url += `?${this.getQueryString(data)}`;
options.params = data;
}

const options = await this.getRequestOptions(method, data);
this.log(`Invocation url: ${url}`);
this.log(`Invoking function ${functionName} with ${method} request`);
return await axios(url, options,);
try {
return await axios(url, options,);
} catch (err) {
const response = err.response;
throw new Error(`HTTP Error: ${response.status} - ${response.data}`);
}
}

private async getUrl(functionName: string) {
Expand Down
1 change: 0 additions & 1 deletion src/shared/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Serverless from "serverless";
import { constants } from "./constants";
import axios from "axios";
const defaultBindingsJson = require("./bindings.json"); // eslint-disable-line @typescript-eslint/no-var-requires

export class BindingUtils {
Expand Down

0 comments on commit 947a305

Please sign in to comment.