Skip to content
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

Parsing [XXX] in error messages, to match serverless 1.0 #133

Merged
merged 2 commits into from
Nov 2, 2016
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
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,19 @@ class Offline {
/* RESPONSE SELECTION (among endpoint's possible responses) */

// Failure handling
let errorStatusCode = 0;
if (err) {

const errorMessage = (err.message || err).toString();

const re = /\[(\d{3})\]/;
const found = errorMessage.match(re);
if (found && found.length > 1) {
errorStatusCode = found[1];
} else {
errorStatusCode = '500';
}

// Mocks Lambda errors
result = {
errorMessage,
Expand Down Expand Up @@ -537,7 +546,6 @@ class Offline {
let statusCode;
if (integration === 'lambda') {
/* RESPONSE TEMPLATE PROCCESSING */

// If there is a responseTemplate, we apply it to the result
const responseTemplates = chosenResponse.responseTemplates;

Expand Down Expand Up @@ -568,8 +576,7 @@ class Offline {
}

/* HAPIJS RESPONSE CONFIGURATION */

statusCode = chosenResponse.statusCode || 200;
const statusCode = errorStatusCode !== 0 ? errorStatusCode : chosenResponse.statusCode || 200;
if (!chosenResponse.statusCode) {
this.printBlankLine();
this.serverlessLog(`Warning: No statusCode found for response "${responseName}".`);
Expand Down
52 changes: 52 additions & 0 deletions test/integration/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,58 @@ describe('Offline', () => {
});
});

context('lambda integration, parse [xxx] as status codes in errors', () => {
it('should set the status code to 500 when no [xxx] is present', (done) => {
const offLine = new OffLineBuilder().addFunctionConfig('index', {
handler: 'users.index',
events: [{
http: {
path: 'index',
method: 'GET',
integration: 'lambda',
response: {
headers: {
'Content-Type': "'text/html'",
},
template: "$input.path('$')",
},
},
}],
}, (event, context, cb) => cb(new Error('Internal Server Error'))).toObject();

offLine.inject('/index', (res) => {
expect(res.headers['content-type']).to.contains('text/html');
expect(res.statusCode).to.eq('500');
done();
});
});

it('should set the status code to 401 when [401] is the prefix of the error message', (done) => {
const offLine = new OffLineBuilder().addFunctionConfig('index', {
handler: 'users.index',
events: [{
http: {
path: 'index',
method: 'GET',
integration: 'lambda',
response: {
headers: {
'Content-Type': "'text/html'",
},
template: "$input.path('$')",
},
},
}],
}, (event, context, cb) => cb(new Error('[401] Unauthorized'))).toObject();

offLine.inject('/index', (res) => {
expect(res.headers['content-type']).to.contains('text/html');
expect(res.statusCode).to.eq('401');
done();
});
});
});

context('[lamda-proxy] Support stageVariables from the stageVariables plugin', () => {
it('should handle custom stage variables declaration', (done) => {
const offLine = new OffLineBuilder().addCustom("stageVariables", {hello: 'Hello World'}).addFunctionHTTP('hello', {
Expand Down