Passed
Push — master ( f80f95...7fc31c )
by Igor
06:13 queued 02:21
created

config.*ErrLoadFailed.Error   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
package config
2
3
import (
4
	"fmt"
5
	"sort"
6
	"strings"
7
8
	validation "github.com/go-ozzo/ozzo-validation/v4"
9
)
10
11
type ErrLoadFailed struct {
12
	Previous error
13
}
14
15
func (err *ErrLoadFailed) Error() string {
16 1
	return fmt.Sprintf("failed to load configuration: %s", err.Previous.Error())
17
}
18
19
func (err *ErrLoadFailed) Unwrap() error {
20
	return err.Previous
21
}
22
23
type ErrInvalidConfiguration struct {
24
	ValidationError error
25
}
26
27
func (err *ErrInvalidConfiguration) Error() string {
28 1
	violations := err.ValidationError.(validation.Errors)
29
30 1
	keys := make([]string, 0, len(violations))
31 1
	for key := range violations {
32 1
		keys = append(keys, key)
33
	}
34 1
	sort.Strings(keys)
35
36 1
	var message strings.Builder
37 1
	message.WriteString("configuration has invalid values: ")
38
39 1
	for i, key := range keys {
40 1
		err := violations[key]
41 1
		if err != nil {
42 1
			if i > 0 {
43 1
				message.WriteString("; ")
44
			}
45 1
			message.WriteString(fmt.Sprintf("invalid option '%s': %s", key, err.Error()))
46
		}
47
	}
48
49 1
	return message.String()
50
}
51