Skip to content
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
40 changes: 30 additions & 10 deletions aws-node-github-webhook-listener/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,48 @@ module.exports.githubWebhookListener = (event, context, callback) => {
const calculatedSig = signRequestBody(token, event.body);

if (typeof token !== 'string') {
errMsg = '[401] must provide a \'GITHUB_WEBHOOK_SECRET\' env variable';
return callback(new Error(errMsg));
errMsg = 'Must provide a \'GITHUB_WEBHOOK_SECRET\' env variable';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}

if (!sig) {
errMsg = '[401] No X-Hub-Signature found on request';
return callback(new Error(errMsg));
errMsg = 'No X-Hub-Signature found on request';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}

if (!githubEvent) {
errMsg = '[422] No X-Github-Event found on request';
return callback(new Error(errMsg));
errMsg = 'No X-Github-Event found on request';
return callback(null, {
statusCode: 422,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}

if (!id) {
errMsg = '[401] No X-Github-Delivery found on request';
return callback(new Error(errMsg));
errMsg = 'No X-Github-Delivery found on request';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}

if (sig !== calculatedSig) {
errMsg = '[401] X-Hub-Signature incorrect. Github webhook token doesn\'t match';
return callback(new Error(errMsg));
errMsg = 'X-Hub-Signature incorrect. Github webhook token doesn\'t match';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}

/* eslint-disable */
Expand Down
6 changes: 5 additions & 1 deletion aws-node-rekognition-analysis-s3-image/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module.exports.imageAnalysis = (event, context, callback) => {
callback(null, response);
})
.catch((error) => {
callback(error, null);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: error.message || 'Internal server error',
});
});
};
43 changes: 34 additions & 9 deletions aws-node-rest-api-mongodb/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ mongoose.Promise = bluebird;

const mongoString = ''; // MongoDB Url

const createErrorResponse = (statusCode, message) => ({
statusCode: statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: message || 'Incorrect id',
});

module.exports.user = (event, context, callback) => {
const db = mongoose.connect(mongoString).connection;
const id = event.pathParameters.id;

if (!validator.isAlphanumeric(id)) throw Error('Incorrect id');
if (!validator.isAlphanumeric(id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
db.close();
return;
}

db.once('open', () => {
UserModel
Expand All @@ -20,7 +30,7 @@ module.exports.user = (event, context, callback) => {
callback(null, { statusCode: 200, body: JSON.stringify(user) });
})
.catch((err) => {
callback(err);
callback(null, createErrorResponse(err.statusCode, err.message));
})
.finally(() => {
// Close db connection or node event loop won't exit , and lambda will timeout
Expand Down Expand Up @@ -51,7 +61,9 @@ module.exports.createUser = (event, context, callback) => {

if (errs) {
console.log(errs);
throw Error('Incorrect user data');
callback(null, createErrorResponse(400, 'Incorrect user data'));
db.close();
return;
}


Expand All @@ -62,7 +74,7 @@ module.exports.createUser = (event, context, callback) => {
callback(null, { statusCode: 200, body: JSON.stringify({ id: user[mongooseId] }) });
})
.catch((err) => {
callback(err);
callback(null, createErrorResponse(err.statusCode, err.message));
})
.finally(() => {
db.close();
Expand All @@ -74,7 +86,11 @@ module.exports.deleteUser = (event, context, callback) => {
const db = mongoose.connect(mongoString).connection;
const id = event.pathParameters.id;

if (!validator.isAlphanumeric(id)) throw Error('Incorrect id');
if (!validator.isAlphanumeric(id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
db.close();
return;
}

db.once('open', () => {
UserModel
Expand All @@ -83,7 +99,7 @@ module.exports.deleteUser = (event, context, callback) => {
callback(null, { statusCode: 200, body: JSON.stringify('Ok') });
})
.catch((err) => {
callback(err);
callback(null, createErrorResponse(err.statusCode, err.message));
})
.finally(() => {
db.close();
Expand All @@ -98,7 +114,11 @@ module.exports.updateUser = (event, context, callback) => {
let errs = {};
let user = {};

if (!validator.isAlphanumeric(id)) throw Error('Incorrect id');
if (!validator.isAlphanumeric(id)) {
callback(null, createErrorResponse(400, 'Incorrect id'));
db.close();
return;
}

user = new UserModel({ _id: id,
name: data.name,
Expand All @@ -109,15 +129,20 @@ module.exports.updateUser = (event, context, callback) => {

errs = user.validateSync();

if (errs) throw Error('Incorrect parameter');
if (errs) {
callback(null, createErrorResponse(400, 'Incorrect parameter'));
db.close();
return;
}

db.once('open', () => {
// UserModel.save() could be used too
UserModel.findByIdAndUpdate(id, user)
.then(() => {
callback(null, { statusCode: 200, body: JSON.stringify('Ok') });
})
.catch((err) => {
callback(err);
callback(err, createErrorResponse(err.statusCode, err.message));
})
.finally(() => {
db.close();
Expand Down
12 changes: 10 additions & 2 deletions aws-node-rest-api-with-dynamodb-and-offline/todos/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module.exports.create = (event, context, callback) => {
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(new Error('Couldn\'t create the todo item.'));
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}

Expand All @@ -28,7 +32,11 @@ module.exports.create = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t create the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb-and-offline/todos/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ module.exports.delete = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t remove the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t remove the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb-and-offline/todos/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ module.exports.get = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t fetch the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb-and-offline/todos/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module.exports.list = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t fetch the todos.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todo item.',
});
return;
}

Expand Down
12 changes: 10 additions & 2 deletions aws-node-rest-api-with-dynamodb-and-offline/todos/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ module.exports.update = (event, context, callback) => {
// validation
if (typeof data.text !== 'string' || typeof data.checked !== 'boolean') {
console.error('Validation Failed');
callback(new Error('Couldn\'t update the todo item.'));
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t update the todo item.',
});
return;
}

Expand All @@ -35,7 +39,11 @@ module.exports.update = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t update the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t update the todo item.',
});
return;
}

Expand Down
12 changes: 10 additions & 2 deletions aws-node-rest-api-with-dynamodb/todos/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ module.exports.create = (event, context, callback) => {
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(new Error('Couldn\'t create the todo item.'));
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}

Expand All @@ -30,7 +34,11 @@ module.exports.create = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t create the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb/todos/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module.exports.delete = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t remove the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t remove the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb/todos/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module.exports.get = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t fetch the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-rest-api-with-dynamodb/todos/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ module.exports.list = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t fetch the todos.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
});
return;
}

Expand Down
12 changes: 10 additions & 2 deletions aws-node-rest-api-with-dynamodb/todos/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ module.exports.update = (event, context, callback) => {
// validation
if (typeof data.text !== 'string' || typeof data.checked !== 'boolean') {
console.error('Validation Failed');
callback(new Error('Couldn\'t update the todo item.'));
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t update the todo item.',
});
return;
}

Expand All @@ -37,7 +41,11 @@ module.exports.update = (event, context, callback) => {
// handle potential errors
if (error) {
console.error(error);
callback(new Error('Couldn\'t update the todo item.'));
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todo item.',
});
return;
}

Expand Down
6 changes: 5 additions & 1 deletion aws-node-stripe-integration/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ module.exports.incoming = (event, context, callback) => {
callback(null, response);
});
} catch (err) {
context.fail(err);
callback(null, {
statusCode: err.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: err.message || 'Internal server error',
});
}
};
Loading