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

Fixed issue with exception injection not throwing the exception #4

Merged
merged 5 commits into from
Feb 13, 2020
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.handler = failureLambda(async (event, context) => {
```
4. Create a parameter in SSM Parameter Store.
```json
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100}
{"isEnabled": false, "failureMode": "latency", "rate": 1, "catchException": false, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100}
```
```bash
aws ssm put-parameter --region eu-north-1 --name failureLambdaConfig --type String --overwrite --value "{\"isEnabled\": false, \"failureMode\": \"latency\", \"rate\": 1, \"minLatency\": 100, \"maxLatency\": 400, \"exceptionMsg\": \"Exception message!\", \"statusCode\": 404, \"diskSpace\": 100}"
Expand All @@ -38,6 +38,7 @@ Edit the values of your parameter in SSM Parameter Store to use the failure inje
* `isEnabled: false` means that the failure injection module is disabled and no failure is injected.
* `failureMode` selects which failure you want to inject. The options are `latency`, `exception` or `statuscode` as explained below.
* `rate` controls the rate of failure. 1 means that failure is injected on all invocations and 0.5 that failure is injected on about half of all invocations.
* `catchException` specifies whether to catch any thrown exceptions or to pass them through to Lambda (**default** is false)
* `minLatency` and `maxLatency` is the span of latency in milliseconds injected into your function when `failureMode` is set to `latency`.
* `exceptionMsg` is the message thrown with the exception created when `failureMode` is set to `exception`.
* `statusCode` is the status code returned by your function when `failureMode` is set to `statuscode`.
Expand All @@ -57,6 +58,10 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack

## Changelog

### 2020-02-08 v0.1.1

* Added a flag to determine whether to catch or release errors before they reach AWS Lambda

### 2019-12-30 v0.1.0

* Added disk space failure.
Expand All @@ -68,4 +73,4 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack

## Authors

**Gunnar Grosch** - [GitHub](https://github.com/gunnargrosch) | [Twitter](https://twitter.com/gunnargrosch) | [LinkedIn](https://www.linkedin.com/in/gunnargrosch/)
**Gunnar Grosch** - [GitHub](https://github.com/gunnargrosch) | [Twitter](https://twitter.com/gunnargrosch) | [LinkedIn](https://www.linkedin.com/in/gunnargrosch/)
9 changes: 7 additions & 2 deletions lib/failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const aws = require('aws-sdk')
const ssm = new aws.SSM()
const childProcess = require('child_process')

async function getConfig () {
async function getConfig() {
try {
let params = {
Name: process.env.FAILURE_INJECTION_PARAM
Expand All @@ -17,10 +17,13 @@ async function getConfig () {
}
var injectFailure = function (fn) {
return async function () {
var catchFlag = true

try {
let configResponse = await getConfig()
let config = JSON.parse(configResponse)
if (config.isEnabled === true && Math.random() < config.rate) {
catchFlag = config.catchException
if (config.failureMode === 'latency') {
let latencyRange = config.maxLatency - config.minLatency
let setLatency = Math.floor(config.minLatency + Math.random() * latencyRange)
Expand All @@ -41,8 +44,10 @@ var injectFailure = function (fn) {
return fn.apply(this, arguments)
} catch (ex) {
console.log(ex)
if (! catchFlag) {
throw ex
}
}
}
}

module.exports = injectFailure