Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

fix response header when lambda function returns errorMessage #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions lib/invoke/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ const invoke = (func, event, lambdaEndpoint, logger) => {
try {
const output = result.stdout ? JSON.parse(result.stdout) : null

if (output && output.errorMessage !== null && output.errorMessage !== undefined) {
return BbPromise.reject(new Error(output.errorMessage))
}

return BbPromise.resolve(output)
} catch (err) {
log(err)
Expand Down
9 changes: 3 additions & 6 deletions lib/invoke/local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('local-invoke', () => {
})
})

it('should fail when errorMessage returned', () => {
it('should invoke when errorMessage returned', () => {
const funcConfig = {
runtime: 'python2.7',
servicePath: '/test/path',
Expand All @@ -144,7 +144,7 @@ describe('local-invoke', () => {

return lambda
.invoke(funcConfig, event)
.catch((err) => {
.then((actual) => {
expect(mockRun.mock.calls.length).toBe(1)
expect(mockRun.mock.calls[0][0]).toEqual({
addEnvVars: true,
Expand All @@ -168,11 +168,8 @@ describe('local-invoke', () => {
],
})

expect(err.message).toEqual('My Test Error')

return true
expect(actual.errorMessage).toEqual('My Test Error')
})
.then(handled => expect(handled).toEqual(true))
})

it('should not fail when stderr returned with no stdout', () => {
Expand Down
5 changes: 5 additions & 0 deletions lib/registry/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const invoke20150331 = (lambda, db, req, res) => {
invokePromise
.then((result) => {
res.status(200)
// If lambda function returns an error, set X-Amz-Function-Error header
// to response that the error was handled.
if (result.errorMessage !== null && result.errorMessage !== undefined) {
res.set('X-Amz-Function-Error', 'Handled')
}
res.send(result)
})
.catch((err) => {
Expand Down
68 changes: 68 additions & 0 deletions lib/registry/invoke.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const BbPromise = require('bluebird')
const invoke = require('./invoke')
const data = require('./data')

jest.mock('./data')

describe('invoke20150331', () => {
it('should return 200 when lambda result is ok', () => {
const mockLambda = {
invoke: () => (
BbPromise.resolve({
result: 'Test',
})
),
}

const requestMock = {
params: {
functionName: 'Test',
},
logger: jest.fn(),
get: () => ('RequestResponse'),
}

const responseMock = {
status: jest.fn(),
send: jest.fn(),
}

data.getFunction.mockReturnValue(BbPromise.resolve({}))
return invoke.invoke20150331(mockLambda, {}, requestMock, responseMock)
.then(() => {
expect(responseMock.status.mock.calls[0][0]).toEqual(200)
data.getFunction.mockClear()
})
})

it('should return \'X-Amz-Function-Error\' when lambda returns errorMessage', () => {
const mockLambda = {
invoke: () => (
BbPromise.resolve({
errorMessage: 'Test',
})
),
}

const requestMock = {
params: {
functionName: 'Test',
},
logger: jest.fn(),
get: () => ('RequestResponse'),
}

const responseMock = {
status: jest.fn(),
set: jest.fn(),
send: jest.fn(),
}

data.getFunction.mockReturnValue(BbPromise.resolve({}))
return invoke.invoke20150331(mockLambda, {}, requestMock, responseMock)
.then(() => {
expect(responseMock.set.mock.calls[0][0]).toEqual('X-Amz-Function-Error')
data.getFunction.mockClear()
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"eslint-plugin-jsx-a11y": "^2.1.0",
"eslint-plugin-react": "^6.1.1",
"jest": "^19.0.2",
"serverless": "^1.0.3"
"serverless": "^1.30.3"
}
}
Loading