aws-core-utils
Advanced tools
Comparing version 8.1.0 to 8.1.1
@@ -28,2 +28,4 @@ 'use strict'; | ||
const cycle = require('./_cycle'); | ||
/** | ||
@@ -125,3 +127,3 @@ * Utilities for generating `handler` functions for and for working with AWS Lambdas that are exposed via API Gateway. | ||
succeedLambdaCallback(callback, response, event, context); | ||
return succeedLambdaCallback(callback, response, event, context); | ||
}) | ||
@@ -133,7 +135,7 @@ .catch(err => { | ||
log(context, LogLevel.WARN, isNotBlank(opts.invalidRequestMsg) ? opts.invalidRequestMsg : 'Invalid request', '-', err.message); | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} else { | ||
// Log the error encountered | ||
log(context, LogLevel.ERROR, isNotBlank(opts.failureMsg) ? opts.failureMsg : 'Failed to execute Lambda', err); | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} | ||
@@ -145,3 +147,3 @@ }); | ||
// Fail the Lambda callback | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} | ||
@@ -296,13 +298,26 @@ } | ||
function succeedLambdaCallback(callback, response, event, context) { | ||
const handler = context && context.handler; | ||
if (handler && handler.useLambdaProxy) { | ||
const statusCode = response && isNotBlank(response.statusCode) ? response.statusCode : 200; | ||
const body = (response && response.body) || response || {}; | ||
const proxyResponse = toLambdaProxyResponse(statusCode, response && response.headers, body, handler.defaultHeaders); | ||
executePreSuccessCallback(proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)); | ||
} else { | ||
executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)); | ||
} | ||
return Promises.try(() => { | ||
const handler = context && context.handler; | ||
if (handler && handler.useLambdaProxy) { | ||
const statusCode = response && isNotBlank(response.statusCode) ? response.statusCode : 200; | ||
const body = (response && response.body) || response || {}; | ||
const proxyResponse = toLambdaProxyResponse(statusCode, response && response.headers, body, handler.defaultHeaders); | ||
return executePreSuccessCallback(proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreSuccessCallback`, err); | ||
return callback(null, proxyResponse); | ||
}); | ||
} else { | ||
return executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreSuccessCallback`, err); | ||
return callback(null, response); | ||
}); | ||
} | ||
}).catch(err => { | ||
console.error(`Unexpected failure during succeedLambdaCallback`, err); | ||
return callback(null, response); | ||
}); | ||
} | ||
@@ -327,27 +342,40 @@ | ||
function failLambdaCallback(callback, error, event, context) { | ||
// Convert the error into an "API" error | ||
const handler = context && context.handler; | ||
const allowedHttpStatusCodes = handler && handler.allowedHttpStatusCodes; | ||
const apiError = appErrors.toAppErrorForApiGateway(error, undefined, undefined, allowedHttpStatusCodes); | ||
return Promises.try(() => { | ||
// Convert the error into an "API" error | ||
const handler = context && context.handler; | ||
const allowedHttpStatusCodes = handler && handler.allowedHttpStatusCodes; | ||
const apiError = appErrors.toAppErrorForApiGateway(error, undefined, undefined, allowedHttpStatusCodes); | ||
// Resolve the AWS request id (if available) | ||
apiError.awsRequestId = trim(apiError.awsRequestId) || trim(error.awsRequestId) || | ||
(context && (trim(context.awsRequestId) || (context.awsContext && trim(context.awsContext.awsRequestId)))) || | ||
undefined; | ||
// Resolve the AWS request id (if available) | ||
apiError.awsRequestId = trim(apiError.awsRequestId) || trim(error.awsRequestId) || | ||
(context && (trim(context.awsRequestId) || (context.awsContext && trim(context.awsContext.awsRequestId)))) || | ||
undefined; | ||
// Resolve the audit reference (if available) | ||
apiError.auditRef = trim(apiError.auditRef) || trim(error.auditRef) || undefined; | ||
// Resolve the audit reference (if available) | ||
apiError.auditRef = trim(apiError.auditRef) || trim(error.auditRef) || undefined; | ||
if (handler && handler.useLambdaProxy) { | ||
const statusCode = apiError.httpStatus; | ||
const body = toCustomOrDefaultErrorResponseBody(apiError, event, context); | ||
const defaultHeaders = handler.defaultHeaders; | ||
const proxyResponse = toLambdaProxyResponse(statusCode, error.headers, body, defaultHeaders); | ||
executePreFailureCallback(apiError, proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)); | ||
} else { | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(JSON.stringify(errorResponse), null)); | ||
} | ||
if (handler && handler.useLambdaProxy) { | ||
const statusCode = apiError.httpStatus; | ||
const body = toCustomOrDefaultErrorResponseBody(apiError, event, context); | ||
const defaultHeaders = handler.defaultHeaders; | ||
const proxyResponse = toLambdaProxyResponse(statusCode, error.headers, body, defaultHeaders); | ||
return executePreFailureCallback(apiError, proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreFailureCallback`, err); | ||
return callback(null, proxyResponse); | ||
}); | ||
} else { | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
return executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(stringify(errorResponse), null)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreFailureCallback`, err); | ||
return callback(stringify(errorResponse), null); | ||
}); | ||
} | ||
}).catch(err => { | ||
console.error(`Unexpected failure during failLambdaCallback`, err); | ||
return callback(stringify(error), null); | ||
}); | ||
} | ||
@@ -372,3 +400,3 @@ | ||
proxyResponse.body = isString(body) ? body : JSON.stringify(body); | ||
proxyResponse.body = isString(body) ? body : stringify(body); | ||
@@ -433,8 +461,10 @@ return proxyResponse; | ||
const json = error && error.toJSON(); | ||
if (json.httpStatus) { | ||
delete json.httpStatus; // don't really need `httpStatus` inside `body` too, since have it in response as `statusCode` | ||
if (json) { | ||
if (json.httpStatus) { | ||
delete json.httpStatus; // don't really need `httpStatus` inside `body` too, since have it in response as `statusCode` | ||
} | ||
if (json.headers) { | ||
delete json.headers; // don't want error's `headers` inside `body`, since have more comprehensive `headers` in response | ||
} | ||
} | ||
if (json.headers) { | ||
delete json.headers; // don't want error's `headers` inside `body`, since have more comprehensive `headers` in response | ||
} | ||
return json; | ||
@@ -455,6 +485,17 @@ } | ||
return JSON.stringify(o); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
log(context, LogLevel.ERROR, err); | ||
return strings.stringify(o); | ||
try { | ||
// First replace any circular references with path references & then retry JSON.stringify again | ||
const decycled = cycle.decycle(o); | ||
return JSON.stringify(decycled); | ||
} | ||
catch (err) { | ||
// Give up and use strings.stringify, which will at least show structure, but may NOT be parseable | ||
log(context, LogLevel.ERROR, err); | ||
return strings.stringify(o); | ||
} | ||
} | ||
} |
## Changes | ||
### 8.1.1 | ||
- Changes to `api-lambdas` and `other-lambdas` modules: | ||
- Changed `succeedLambdaCallback` and `failLambdaCallback` functions to return promises of the callback results | ||
- Added more error handling to `succeedLambdaCallback` and `failLambdaCallback` functions in an attempt to catch and | ||
resolve issues encountered where callback is not being invoked | ||
### 8.1.0 | ||
@@ -4,0 +10,0 @@ - Changes to `api-lambdas` module: |
@@ -24,2 +24,4 @@ 'use strict'; | ||
const cycle = require('./_cycle'); | ||
/** | ||
@@ -106,3 +108,3 @@ * Utilities for generating `handler` functions for and for working with "other" AWS Lambdas that are NOT exposed via | ||
succeedLambdaCallback(callback, response, event, context); | ||
return succeedLambdaCallback(callback, response, event, context); | ||
}) | ||
@@ -114,7 +116,7 @@ .catch(err => { | ||
log(context, LogLevel.WARN, isNotBlank(opts.invalidRequestMsg) ? opts.invalidRequestMsg : 'Invalid request', '-', err.message); | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} else { | ||
// Log the error encountered | ||
log(context, LogLevel.ERROR, isNotBlank(opts.failureMsg) ? opts.failureMsg : 'Failed to execute Lambda', err); | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} | ||
@@ -126,3 +128,3 @@ }); | ||
// Fail the Lambda callback | ||
failLambdaCallback(callback, err, event, context); | ||
return failLambdaCallback(callback, err, event, context); | ||
} | ||
@@ -264,4 +266,8 @@ } | ||
function succeedLambdaCallback(callback, response, event, context) { | ||
executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)); | ||
return executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreSuccessCallback`, err); | ||
return callback(null, response); | ||
}); | ||
} | ||
@@ -284,17 +290,25 @@ | ||
function failLambdaCallback(callback, error, event, context) { | ||
// Convert the error into an "API" error with an HTTP status code | ||
const apiError = appErrors.toAppError(error); | ||
return Promises.try(() => { | ||
// Convert the error into an "API" error with an HTTP status code | ||
const apiError = appErrors.toAppError(error); | ||
// Resolve the AWS request id (if available) | ||
apiError.awsRequestId = trim(apiError.awsRequestId) || trim(error.awsRequestId) || | ||
(context && (trim(context.awsRequestId) || (context.awsContext && trim(context.awsContext.awsRequestId)))) || | ||
undefined; | ||
// Resolve the AWS request id (if available) | ||
apiError.awsRequestId = trim(apiError.awsRequestId) || trim(error.awsRequestId) || | ||
(context && (trim(context.awsRequestId) || (context.awsContext && trim(context.awsContext.awsRequestId)))) || | ||
undefined; | ||
// Resolve the audit reference (if available) | ||
apiError.auditRef = trim(apiError.auditRef) || trim(error.auditRef) || undefined; | ||
// Resolve the audit reference (if available) | ||
apiError.auditRef = trim(apiError.auditRef) || trim(error.auditRef) || undefined; | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(JSON.stringify(errorResponse), null)); | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
return executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(stringify(errorResponse), null)) | ||
.catch(err => { | ||
console.error(`Unexpected failure after executePreFailureCallback`, err); | ||
return callback(stringify(errorResponse), null); | ||
}); | ||
}).catch(err => { | ||
console.error(`Unexpected failure during failLambdaCallback`, err); | ||
return callback(stringify(error), null); | ||
}); | ||
} | ||
@@ -338,6 +352,17 @@ | ||
return JSON.stringify(o); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
log(context, LogLevel.ERROR, err); | ||
return strings.stringify(o); | ||
try { | ||
// First replace any circular references with path references & then retry JSON.stringify again | ||
const decycled = cycle.decycle(o); | ||
return JSON.stringify(decycled); | ||
} | ||
catch (err) { | ||
// Give up and use strings.stringify, which will at least show structure, but may NOT be parseable | ||
log(context, LogLevel.ERROR, err); | ||
return strings.stringify(o); | ||
} | ||
} | ||
} |
{ | ||
"name": "aws-core-utils", | ||
"version": "8.1.0", | ||
"version": "8.1.1", | ||
"description": "Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -1,2 +0,2 @@ | ||
# aws-core-utils v8.1.0 | ||
# aws-core-utils v8.1.1 | ||
@@ -3,0 +3,0 @@ Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
324182
24
4469