Skip to content

refactor: polish http error response #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/function_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const wrapOpenFunction = (

Promise.resolve()
.then(() => userFunction(ctx, req.body))
.then(() => res.end())
.then(result => callback(null, result))
.catch(err => callback(err, undefined));
};

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

/**
* List of function signature types that are supported by the framework.
Expand Down
12 changes: 9 additions & 3 deletions test/integration/cloud_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
// limitations under the License.

import * as assert from 'assert';
import * as functions from '../../src/index';

import * as supertest from 'supertest';
import * as sinon from 'sinon';

import * as functions from '../../src/index';
import {getTestServer} from '../../src/testing';
import * as supertest from 'supertest';
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';

// A structured CloudEvent
const TEST_CLOUD_EVENT = {
Expand Down Expand Up @@ -316,7 +319,10 @@ describe('CloudEvent Function', () => {
.post('/')
.send(TEST_CLOUD_EVENT)
.expect(res => {
assert.deepStrictEqual(res.headers['x-google-status'], 'error');
assert.deepStrictEqual(
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
'error'
);
assert.deepStrictEqual(res.body, {});
})
.expect(500);
Expand Down
21 changes: 14 additions & 7 deletions test/integration/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as supertest from 'supertest';

import * as functions from '../../src/index';
import {getTestServer} from '../../src/testing';
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';

describe('HTTP Function', () => {
let callCount = 0;
Expand Down Expand Up @@ -99,13 +100,19 @@ describe('HTTP Function', () => {
testData.forEach(test => {
it(test.name, async () => {
const st = supertest(getTestServer('testHttpFunction'));
await (test.httpVerb === 'GET'
? st.get(test.path)
: st.post(test.path).send({text: 'hello'})
)
.set('Content-Type', 'application/json')
.expect(test.expectedBody)
.expect(test.expectedStatus);
try {
await (test.httpVerb === 'GET'
? st.get(test.path)
: st.post(test.path).send({text: 'hello'})
)
.set('Content-Type', 'application/json')
.expect(test.expectedBody)
.expect(test.expectedStatus)
.expect(FUNCTION_STATUS_HEADER_FIELD, 'crash');
} catch (err) {
test.expectedStatus === 500 && assert(err);
}

assert.strictEqual(callCount, test.expectedCallCount);
});
});
Expand Down
13 changes: 11 additions & 2 deletions test/integration/http_binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {OpenFunctionContext} from '../../src/openfunction/function_context';

import {OpenFunctionRuntime} from '../../src/functions';
import {getServer} from '../../src/server';
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';

const TEST_CONTEXT: OpenFunctionContext = {
name: 'test-context',
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('OpenFunction - HTTP Binding', () => {
{name: 'Save data', operation: 'create', listable: true},
{name: 'Get data', operation: 'get', listable: true},
{name: 'Delete data', operation: 'delete', listable: false},
{name: 'Error data', operation: '', listable: false},
];

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

const server = getServer(
async (ctx: OpenFunctionRuntime, data: {}) => {
if (!test.operation) throw new Error('I crashed');

await ctx.send(data);
ctx.res?.send(data);
},
Expand All @@ -93,9 +97,14 @@ describe('OpenFunction - HTTP Binding', () => {
await supertest(server)
.post('/')
.send(TEST_PAYLOAD)
.expect(200)
.expect(test.operation ? 200 : 500)
.expect(res => {
deepStrictEqual(res.body, TEST_PAYLOAD);
!test.operation
? deepStrictEqual(
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
'error'
)
: deepStrictEqual(res.body, TEST_PAYLOAD);
});

forEach(context.outputs, output => {
Expand Down
12 changes: 9 additions & 3 deletions test/integration/legacy_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
// limitations under the License.

import * as assert from 'assert';
import * as functions from '../../src/functions';

import * as supertest from 'supertest';
import * as sinon from 'sinon';

import * as functions from '../../src/index';
import {getServer} from '../../src/server';
import * as supertest from 'supertest';
import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types';

const TEST_CLOUD_EVENT = {
specversion: '1.0',
Expand Down Expand Up @@ -214,7 +217,10 @@ describe('Event Function', () => {
})
.set({'Content-Type': 'application/json'})
.expect(res => {
assert.deepStrictEqual(res.headers['x-google-status'], 'error');
assert.deepStrictEqual(
res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()],
'error'
);
assert.deepStrictEqual(res.body, {});
})
.expect(500);
Expand Down