Skip to content

Commit

Permalink
feat(Post Processing): stop request chain if post processing error
Browse files Browse the repository at this point in the history
  • Loading branch information
visola committed Sep 26, 2019
1 parent e87744c commit e463529
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pkg/request/executor.go
Expand Up @@ -41,9 +41,9 @@ func ExecuteRequestLoop(executionContext ExecutionContext) ([]ExecutedRequestRes
return nil, sessionErr
}

currentConfiguredRequest, processError := replaceRequestVariables(currentConfiguredRequest, mergedProfiles, executionContext)
if processError != nil {
return nil, processError
currentConfiguredRequest, replaceVariablesError := replaceRequestVariables(currentConfiguredRequest, mergedProfiles, executionContext)
if replaceVariablesError != nil {
return nil, replaceVariablesError
}

response, executeErr := executeRequest(client, currentConfiguredRequest, executionContext.Session)
Expand All @@ -62,6 +62,7 @@ func ExecuteRequestLoop(executionContext ExecutionContext) ([]ExecutedRequestRes
result[len(result)-1].PostProcessOutput = postProcessResult.Output
if postProcessError != nil {
result[len(result)-1].PostProcessError = postProcessError.Error()
break
}

if len(postProcessResult.Requests) > 0 {
Expand Down
17 changes: 9 additions & 8 deletions pkg/request/post_process.go
Expand Up @@ -58,7 +58,7 @@ func createAddVariableFunction(executionContext *ExecutionContext) func(string,
}
}

func createAddRequestFunction(context *PostProcessContext, executionContext *ExecutionContext) func(otto.Value) {
func createAddRequestFunction(vm *otto.Otto, context *PostProcessContext, executionContext *ExecutionContext) func(otto.Value) {
return func(value otto.Value) {
unconfiguredRequest := Request{}
requestName := ""
Expand All @@ -75,16 +75,16 @@ func createAddRequestFunction(context *PostProcessContext, executionContext *Exe
if value.IsObject() {
toAddAsMap, err := value.Export()
if err != nil {
context.Output += fmt.Sprintf("Error while converting object to map: %s\n%s\n", value, err.Error())
log.Error("Error while converting request object to map.", err)
return
message := fmt.Sprintf("Error while converting object to map: %s\n%s\n", value, err.Error())
panic(vm.MakeCustomError("MappingError", message))
}

var toAdd Request
if err := mapstructure.Decode(toAddAsMap, &toAdd); err != nil {
context.Output += fmt.Sprintf("Error while converting map to request object.\n%s\n", err.Error())
log.Error("Error while converting map to request object.", err)
return
message := fmt.Sprintf("Error while converting map to request object.\n%s\n", err.Error())
panic(vm.MakeCustomError("ConversionError", message))
}

unconfiguredRequest = toAdd
Expand All @@ -95,11 +95,12 @@ func createAddRequestFunction(context *PostProcessContext, executionContext *Exe

configuredRequest, err := ConfigureRequestSimple(unconfiguredRequest, &mergedProfile, requestName)
if err != nil {
context.Output += fmt.Sprintf("Error while configuring request: %s\n", err.Error())
return
message := fmt.Sprintf("Error while configuring request: %s\n", err.Error())
panic(vm.MakeCustomError("ConfigurationError", message))
}

context.Requests = append(context.Requests, *configuredRequest)
return
}
}

Expand All @@ -122,7 +123,7 @@ func preparePostProcessContext(vm *otto.Otto, executionContext *ExecutionContext
}

vm.Set("addVariable", createAddVariableFunction(executionContext))
vm.Set("addRequest", createAddRequestFunction(context, executionContext))
vm.Set("addRequest", createAddRequestFunction(vm, context, executionContext))
vm.Set("print", createPrintFunction(context))
vm.Set("println", createPrintlnFunction(context))

Expand Down

0 comments on commit e463529

Please sign in to comment.