Updates lambda functions with more robust err handling

This commit is contained in:
Alicia Sykes
2023-07-09 23:23:50 +01:00
parent e37474e0d4
commit 9f06802a50
13 changed files with 203 additions and 167 deletions

View File

@@ -2,21 +2,32 @@ const dns = require('dns');
/* Lambda function to fetch the IP address of a given URL */
exports.handler = function (event, context, callback) {
const addressParam = event.queryStringParameters.address;
const addressParam = event.queryStringParameters.url;
if (!addressParam) {
callback(null, errorResponse('Address parameter is missing.'));
return;
}
const address = decodeURIComponent(addressParam)
.replaceAll('https://', '')
.replaceAll('http://', '');
.replaceAll('https://', '')
.replaceAll('http://', '');
dns.lookup(address, (err, ip, family) => {
if (err) {
callback(null, {
statusCode: 405,
body: JSON.stringify(err),
})
callback(null, errorResponse(err.message));
} else {
callback(err, {
callback(null, {
statusCode: 200,
body: JSON.stringify({ip, family}),
body: JSON.stringify({ ip, family }),
});
}
});
};
const errorResponse = (message, statusCode = 444) => {
return {
statusCode: statusCode,
body: JSON.stringify({ error: message }),
};
};