-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
51 lines (41 loc) · 1.01 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package chaos
import (
"encoding/json"
"fmt"
"math/rand"
"time"
)
type errorSpec struct {
statusCode int
message string
probability float64
}
func (s *errorSpec) UnmarshalJSON(data []byte) error {
spec := struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
Probability float64 `json:"p"`
}{}
if err := json.Unmarshal(data, &spec); err != nil {
return err
}
s.statusCode = spec.StatusCode
s.message = spec.Message
s.probability = spec.Probability
if s.statusCode < 100 || s.statusCode > 600 {
return fmt.Errorf("error status code parameter value must be 100 < n < 600 ")
}
if s.probability < 0 || s.probability > 1 {
return fmt.Errorf("probability parameter value must be 0 < p < 1 ")
}
return nil
}
func (s *spec) injectError() (bool, int, string) {
if s.err != nil {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
if p := rnd.Float64(); p > 1-s.err.probability {
return true, s.err.statusCode, s.err.message
}
}
return false, 0, ""
}