Skip to content

Commit dd57ba0

Browse files
committed
Throw more helpful error if http output is incorrect (#271)
* Throw more helpful error if http output is incorrect * remove unused import * rename tests
1 parent eff3932 commit dd57ba0

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/WorkerChannel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class WorkerChannel implements IWorkerChannel {
106106
if (version.startsWith("v8.")) {
107107
let msg = "Incompatible Node.js version. The version you are using (" + version + ") is not supported with Azure Functions V3. Please use one of the following major versions: 10, 12.";
108108
systemError(msg);
109-
throw msg;
109+
throw new InternalException(msg);
110110
}
111111
}
112112

src/converters/RpcHttpConverters.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function fromRpcHttpBody(body: rpc.ITypedData) {
5252
* @param inputMessage An HTTP response object
5353
*/
5454
export function toRpcHttp(inputMessage): rpc.ITypedData {
55+
// Check if we will fail to find any of these
56+
if (typeof inputMessage !== 'object' || Array.isArray(inputMessage)) {
57+
throw new Error("The HTTP response must be an 'object' type that can include properties such as 'body', 'status', and 'headers'. Learn more: https://go.microsoft.com/fwlink/?linkid=2112563");
58+
}
59+
5560
let httpMessage: rpc.IRpcHttp = inputMessage;
5661
httpMessage.headers = toRpcHttpHeaders(inputMessage.headers);
5762
httpMessage.cookies = toRpcHttpCookieList(inputMessage.cookies || []);

test/RpcHttpConverters.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toRpcHttpCookieList } from '../src/converters';
1+
import { toRpcHttpCookieList, toRpcHttp } from '../src/converters';
22
import { Cookie } from "../types/public/Interfaces";
33
import { expect } from 'chai';
44
import * as sinon from 'sinon';
@@ -43,7 +43,7 @@ describe('Rpc Converters', () => {
4343
expect((<any>rpcCookies[2].expires).value.seconds).to.equal(819199440);
4444
});
4545

46-
it('throws on invalid input', () => {
46+
it('throws on invalid cookie input', () => {
4747
expect(() => {
4848
let cookieInputs = [
4949
{
@@ -72,4 +72,18 @@ describe('Rpc Converters', () => {
7272
toRpcHttpCookieList(<Cookie[]>cookieInputs);
7373
}).to.throw("");
7474
});
75+
76+
it('throws on array as http response', () => {
77+
expect(() => {
78+
let response = ["one", 2, "3"];
79+
toRpcHttp(response);
80+
}).to.throw("The HTTP response must be an 'object' type that can include properties such as 'body', 'status', and 'headers'. Learn more: https://go.microsoft.com/fwlink/?linkid=2112563");
81+
});
82+
83+
it('throws on string as http response', () => {
84+
expect(() => {
85+
let response = "My output string";
86+
toRpcHttp(response);
87+
}).to.throw("The HTTP response must be an 'object' type that can include properties such as 'body', 'status', and 'headers'. Learn more: https://go.microsoft.com/fwlink/?linkid=2112563");
88+
});
7589
})

0 commit comments

Comments
 (0)