Skip to content

Commit cc9ed42

Browse files
authored
Merge pull request #43 from OpenFunction/feature/async-error-ouput
refactor: polish http error response
2 parents 829f69e + acc5c56 commit cc9ed42

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

src/function_wrappers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const wrapOpenFunction = (
138138

139139
Promise.resolve()
140140
.then(() => userFunction(ctx, req.body))
141-
.then(() => res.end())
141+
.then(result => callback(null, result))
142142
.catch(err => callback(err, undefined));
143143
};
144144

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// HTTP header field that is added to Worker response to signalize problems with
1616
// executing the client function.
17-
export const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status';
17+
export const FUNCTION_STATUS_HEADER_FIELD = 'X-OpenFunction-Status';
1818

1919
/**
2020
* List of function signature types that are supported by the framework.

test/integration/cloud_event.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
// limitations under the License.
1414

1515
import * as assert from 'assert';
16-
import * as functions from '../../src/index';
16+
17+
import * as supertest from 'supertest';
1718
import * as sinon from 'sinon';
19+
20+
import * as functions from '../../src/index';
1821
import {getTestServer} from '../../src/testing';
19-
import * as supertest from 'supertest';
22+
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';
2023

2124
// A structured CloudEvent
2225
const TEST_CLOUD_EVENT = {
@@ -316,7 +319,10 @@ describe('CloudEvent Function', () => {
316319
.post('/')
317320
.send(TEST_CLOUD_EVENT)
318321
.expect(res => {
319-
assert.deepStrictEqual(res.headers['x-google-status'], 'error');
322+
assert.deepStrictEqual(
323+
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
324+
'error'
325+
);
320326
assert.deepStrictEqual(res.body, {});
321327
})
322328
.expect(500);

test/integration/http.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as supertest from 'supertest';
1818

1919
import * as functions from '../../src/index';
2020
import {getTestServer} from '../../src/testing';
21+
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';
2122

2223
describe('HTTP Function', () => {
2324
let callCount = 0;
@@ -99,13 +100,19 @@ describe('HTTP Function', () => {
99100
testData.forEach(test => {
100101
it(test.name, async () => {
101102
const st = supertest(getTestServer('testHttpFunction'));
102-
await (test.httpVerb === 'GET'
103-
? st.get(test.path)
104-
: st.post(test.path).send({text: 'hello'})
105-
)
106-
.set('Content-Type', 'application/json')
107-
.expect(test.expectedBody)
108-
.expect(test.expectedStatus);
103+
try {
104+
await (test.httpVerb === 'GET'
105+
? st.get(test.path)
106+
: st.post(test.path).send({text: 'hello'})
107+
)
108+
.set('Content-Type', 'application/json')
109+
.expect(test.expectedBody)
110+
.expect(test.expectedStatus)
111+
.expect(FUNCTION_STATUS_HEADER_FIELD, 'crash');
112+
} catch (err) {
113+
test.expectedStatus === 500 && assert(err);
114+
}
115+
109116
assert.strictEqual(callCount, test.expectedCallCount);
110117
});
111118
});

test/integration/http_binding.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {OpenFunctionContext} from '../../src/openfunction/function_context';
99

1010
import {OpenFunctionRuntime} from '../../src/functions';
1111
import {getServer} from '../../src/server';
12+
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';
1213

1314
const TEST_CONTEXT: OpenFunctionContext = {
1415
name: 'test-context',
@@ -72,6 +73,7 @@ describe('OpenFunction - HTTP Binding', () => {
7273
{name: 'Save data', operation: 'create', listable: true},
7374
{name: 'Get data', operation: 'get', listable: true},
7475
{name: 'Delete data', operation: 'delete', listable: false},
76+
{name: 'Error data', operation: '', listable: false},
7577
];
7678

7779
testData.forEach(test => {
@@ -83,6 +85,8 @@ describe('OpenFunction - HTTP Binding', () => {
8385

8486
const server = getServer(
8587
async (ctx: OpenFunctionRuntime, data: {}) => {
88+
if (!test.operation) throw new Error('I crashed');
89+
8690
await ctx.send(data);
8791
ctx.res?.send(data);
8892
},
@@ -93,9 +97,14 @@ describe('OpenFunction - HTTP Binding', () => {
9397
await supertest(server)
9498
.post('/')
9599
.send(TEST_PAYLOAD)
96-
.expect(200)
100+
.expect(test.operation ? 200 : 500)
97101
.expect(res => {
98-
deepStrictEqual(res.body, TEST_PAYLOAD);
102+
!test.operation
103+
? deepStrictEqual(
104+
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
105+
'error'
106+
)
107+
: deepStrictEqual(res.body, TEST_PAYLOAD);
99108
});
100109

101110
forEach(context.outputs, output => {

test/integration/legacy_event.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
// limitations under the License.
1414

1515
import * as assert from 'assert';
16-
import * as functions from '../../src/functions';
16+
17+
import * as supertest from 'supertest';
1718
import * as sinon from 'sinon';
19+
20+
import * as functions from '../../src/index';
1821
import {getServer} from '../../src/server';
19-
import * as supertest from 'supertest';
22+
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';
2023

2124
const TEST_CLOUD_EVENT = {
2225
specversion: '1.0',
@@ -214,7 +217,10 @@ describe('Event Function', () => {
214217
})
215218
.set({'Content-Type': 'application/json'})
216219
.expect(res => {
217-
assert.deepStrictEqual(res.headers['x-google-status'], 'error');
220+
assert.deepStrictEqual(
221+
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
222+
'error'
223+
);
218224
assert.deepStrictEqual(res.body, {});
219225
})
220226
.expect(500);

0 commit comments

Comments
 (0)