Passed
Push — master ( a05944...01f9ed )
by Igor
20:05 queued 13:18
created

config.*ErrInvalidConfiguration.Error   A

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 15
nop 0
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 5
rs 9.1832
c 0
b 0
f 0
1
package config
2
3
import (
4
	"fmt"
5
	"strings"
6
7
	"github.com/muonsoft/validation"
8
)
9
10
type LoadingFailedError struct {
11
	Previous error
12
}
13
14
func (err *LoadingFailedError) Error() string {
15 1
	return fmt.Sprintf("failed to load configuration: %s", err.Previous.Error())
16
}
17
18
func (err *LoadingFailedError) Unwrap() error {
19
	return err.Previous
20
}
21
22
type InvalidConfigurationError struct {
23
	ValidationError error
24
}
25
26
func (err *InvalidConfigurationError) Error() string {
27 1
	violations, ok := validation.UnwrapViolationList(err.ValidationError)
28 1
	if !ok {
29
		return fmt.Sprintf("failed to validate configuration: %s", err.ValidationError)
30
	}
31
32 1
	var message strings.Builder
33 1
	message.WriteString("configuration has invalid values: ")
34
35 1
	for violation := violations.First(); violation != nil; violation = violation.Next() {
36 1
		if violation != violations.First() {
37 1
			message.WriteString("; ")
38
		}
39 1
		message.WriteString(fmt.Sprintf("invalid option '%s': %s", violation.PropertyPath().String(), violation.Message()))
40
	}
41
42 1
	return message.String()
43
}
44