Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Feb 17, 2016
1 parent e44929d commit 42fd6e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
module.exports = function(ServerlessPlugin, serverlessPath) {

const path = require('path');
const context = require(path.join(serverlessPath, 'utils', 'context'));
const SCli = require(path.join(serverlessPath, 'utils', 'cli'));
const SUtils = require(path.join(serverlessPath, 'utils'));
const Hapi = require('hapi');
// const SUtils = require(path.join(serverlessPath, 'utils'));
const SCli = require(path.join(serverlessPath, 'utils', 'cli'));
const context = require(path.join(serverlessPath, 'utils', 'context'));

return class Offline extends ServerlessPlugin {

Expand Down Expand Up @@ -50,6 +50,7 @@ module.exports = function(ServerlessPlugin, serverlessPath) {
port: this.port
});

// Prefix must start and end with '/'
let prefix = this.evt.prefix || '/';

if (!prefix.startsWith('/')) prefix = '/' + prefix;
Expand All @@ -70,9 +71,9 @@ module.exports = function(ServerlessPlugin, serverlessPath) {
const handlerParts = fun.handler.split('/').pop().split('.');
const handlerPath = path.join(fun._config.fullPath, handlerParts[0] + '.js');


// Add a route for each endpoint
fun.endpoints.forEach(endpoint => {
// const { method, path } = endpoint;

const method = endpoint.method;
const epath = endpoint.path;
const path = this.prefix + (epath.startsWith('/') ? epath.slice(1) : epath);
Expand All @@ -93,27 +94,35 @@ module.exports = function(ServerlessPlugin, serverlessPath) {
handler = require(handlerPath)[handlerParts[1]];
} catch(err) {
SCli.log(`Error while loading ${handlerPath}: ${err}`);
throw err ;
if (err.stack) console.log(err.stack);
}

const event = Object.assign({ isServerlessOffline: true }, request);

// Call the handler
handler(event, context(fun.name, (err, result) => {
let finalResponse;
let finalResult;

const responsesKeys = Object.keys(endpoint.responses);

// Error handling
if (err) {
finalResult = { errorMessage: err };
const errString = err.toString();

SCli.log(`Error: ${errString}`);
if (err.stack) console.log(err.stack);

finalResult = { error: errString };

responsesKeys.forEach(key => {
const x = endpoint.responses[key];
if (!finalResponse && key !== 'default' && x.selectionPattern && errString.match(x.selectionPattern)) {
finalResponse = x;
}
});

if (!finalResponse) finalResponse = { statusCode: 500 };
}

finalResponse = finalResponse || endpoint.responses['default'];
Expand All @@ -122,7 +131,17 @@ module.exports = function(ServerlessPlugin, serverlessPath) {
serverResponse.statusCode = finalResponse.statusCode;
serverResponse.source = finalResult;

SCli.log(`[${finalResponse.statusCode}] ${finalResult}`);
let whatToLog = finalResult;

try {
whatToLog = JSON.stringify(finalResult);
}
catch(err) {
SCli.log(`Error while parsing result:`);
console.log(err.stack);
}

SCli.log(`[${finalResponse.statusCode}] ${whatToLog}`);

serverResponse.send();
}));
Expand Down Expand Up @@ -160,7 +179,6 @@ module.exports = function(ServerlessPlugin, serverlessPath) {
.then(this._createServer.bind(this))
.then(this._registerLambdas.bind(this))
.then(this._listen.bind(this));
// .then(() =>this.evt);
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-offline",
"version": "0.1.1",
"version": "0.1.2",
"description": "Simulates your API Gateway ressources locally to call your lambda functions offline",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 42fd6e9

Please sign in to comment.