-
Notifications
You must be signed in to change notification settings - Fork 94
/
errors.go
96 lines (84 loc) · 3.32 KB
/
errors.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* @license
* Copyright 2023 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graph
import (
"fmt"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/template"
"gonum.org/v1/gonum/graph"
"strings"
)
// SortingErrors is a slice of errors that implements the error interface.
// It may contain general errors as well as CyclicDependencyError.
type SortingErrors []error
func (errs SortingErrors) Error() string {
b := strings.Builder{}
for _, e := range errs {
_, _ = b.WriteString(fmt.Sprintf("%s\n", e.Error()))
}
return b.String()
}
// CyclicDependencyError is returned if sorting a graph failed due to cyclic dependencies between configurations.
type CyclicDependencyError struct {
//The Environment for which the error occurred.
Environment string `json:"environment"`
//A slice of all dependency cycles between configurations as slices of DependencyLocation. Each cycle slice is returned in order of dependencies.
ConfigsInDependencyCycle [][]DependencyLocation `json:"configsInDependencyCycle"`
}
// DependencyLocation is a short from location pointing to the coordinate and (if available) file of the configuration.
type DependencyLocation struct {
// The coordinate.Coordinate of the configuration that is part of a Dependency Cycle
Coordinate coordinate.Coordinate `json:"coordinate"`
// The filepath this configuration was loaded from. May be empty.
Filepath string `json:"filepath,omitempty"`
}
func (e CyclicDependencyError) Error() string {
b := strings.Builder{}
_, _ = b.WriteString(fmt.Sprintf("There are %d dependency cycles between the configurations.\n", len(e.ConfigsInDependencyCycle)))
for _, cycle := range e.ConfigsInDependencyCycle {
_, _ = b.WriteString("Please check the following configuration's references and break the cycle:\n")
for _, c := range cycle {
_, _ = b.WriteString(fmt.Sprintf("%q", c.Coordinate))
if c.Filepath != "" {
_, _ = b.WriteString(fmt.Sprintf(" (%s)", c.Filepath))
}
_, _ = b.WriteString(" -> ")
}
_, _ = b.WriteString(fmt.Sprintf("%q", cycle[0].Coordinate))
}
return b.String()
}
func newCyclicDependencyError(environment string, cycles [][]graph.Node) CyclicDependencyError {
cfgCycles := make([][]DependencyLocation, len(cycles))
for i, cycle := range cycles {
cfgCycles[i] = make([]DependencyLocation, len(cycle))
for j, node := range cycle {
coord := node.(ConfigNode).Config.Coordinate
filepath := ""
if t, ok := node.(ConfigNode).Config.Template.(*template.FileBasedTemplate); ok {
filepath = t.FilePath()
}
cfgCycles[i][j] = DependencyLocation{
Coordinate: coord,
Filepath: filepath,
}
}
}
return CyclicDependencyError{
Environment: environment,
ConfigsInDependencyCycle: cfgCycles,
}
}