Skip to content

Commit

Permalink
fix json injection err check
Browse files Browse the repository at this point in the history
  • Loading branch information
kenanfarukcakir committed Jan 11, 2023
1 parent 90fac4c commit c3f0b3c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions core/scenario/scripting/injection/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (ei *EnvironmentInjector) InjectEnv(text string, envs map[string]interface{
}

errors = append(errors,
fmt.Errorf("%s could not be found in vars global and extracted from previous steps", truncated))
fmt.Errorf("%s could not be found in vars global and extracted from previous steps: %v", truncated, err))
return s
}

Expand All @@ -109,7 +109,10 @@ func (ei *EnvironmentInjector) InjectEnv(text string, envs map[string]interface{
if json.Valid(bText) {
if ei.jr.Match(bText) {
replacedBytes := ei.jr.ReplaceAllFunc(bText, injectToJsonByteFunc)
return string(replacedBytes), nil
if len(errors) == 0 {
return string(replacedBytes), nil
}
return "", unifyErrors(errors)
}
}

Expand Down Expand Up @@ -198,14 +201,18 @@ func (ei *EnvironmentInjector) getEnv(envs map[string]interface{}, key string) (
var err error
var val interface{}

if strings.HasPrefix(key, "rand(") && strings.HasSuffix(key, ")") { // get random val from []interface{}
pickRand := strings.HasPrefix(key, "rand(") && strings.HasSuffix(key, ")")
if pickRand {
key = key[5 : len(key)-1]
var exists bool
val, exists = envs[key]
if !exists {
err = fmt.Errorf("env not found")
}
}

var exists bool
val, exists = envs[key]
if !exists {
err = fmt.Errorf("env not found")
}

if pickRand {
switch v := val.(type) {
case []interface{}:
val = v[rand.Intn(len(v))]
Expand All @@ -220,13 +227,6 @@ func (ei *EnvironmentInjector) getEnv(envs map[string]interface{}, key string) (
default:
err = fmt.Errorf("can not perform rand() operation on non-array value")
}

} else {
var exists bool
val, exists = envs[key]
if !exists {
err = fmt.Errorf("env not found")
}
}

return val, err
Expand Down

0 comments on commit c3f0b3c

Please sign in to comment.