You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// SeverityLevel represents the severity of an error.
|
|
type SeverityLevel string
|
|
|
|
const (
|
|
// Critical errors prevent the configuration from being valid.
|
|
Critical SeverityLevel = "CRITICAL"
|
|
// Warning are issues that are problematic but may not break the configuration.
|
|
Warning SeverityLevel = "WARNING"
|
|
// Note are non-critical messages or hints.
|
|
Note SeverityLevel = "NOTE"
|
|
)
|
|
|
|
// ValidationErrorDetail stores data about one error
|
|
type ValidationErrorDetail struct {
|
|
Code ErrorCode // Error code
|
|
Message string // Text of error
|
|
Line int // Number of line, where error appeared
|
|
Column int // Column
|
|
EndLine int // Endline
|
|
EndColumn int // EndColumn
|
|
Severity SeverityLevel // Severity leevel of an error
|
|
}
|
|
|
|
type UnknownKeyError struct {
|
|
Key string
|
|
Line int
|
|
Column int
|
|
EndLine int
|
|
EndColumn int
|
|
}
|
|
|
|
// ValidationError represents all errors
|
|
type ValidationError struct {
|
|
Errors []ValidationErrorDetail
|
|
}
|
|
|
|
// Error returns string of all errors
|
|
func (ve *ValidationError) Error() string {
|
|
var sb strings.Builder
|
|
sb.WriteString("Validation errors found:\n")
|
|
for i, errDetail := range ve.Errors {
|
|
sb.WriteString(fmt.Sprintf("%d) [%s] (%s) (Line %d, Col %d): %s\n",
|
|
i+1, errDetail.Code, errDetail.Severity, errDetail.Line, errDetail.Column, errDetail.Message))
|
|
}
|
|
return sb.String()
|
|
}
|
|
|
|
// AddDetail adds new error in a list
|
|
func (ve *ValidationError) AddDetail(code ErrorCode, message string, line, column int) {
|
|
ve.Errors = append(ve.Errors, ValidationErrorDetail{
|
|
Code: code,
|
|
Message: message,
|
|
Line: line,
|
|
Column: column,
|
|
Severity: ErrorSeverity[code],
|
|
})
|
|
}
|
|
|
|
// AddDetailWithSpan adds a new error detail with a span.
|
|
func (ve *ValidationError) AddDetailWithSpan(code ErrorCode, message string, line, col, endLine, endCol int) {
|
|
ve.Errors = append(ve.Errors, ValidationErrorDetail{
|
|
Code: code,
|
|
Message: message,
|
|
Line: line,
|
|
Column: col,
|
|
EndLine: endLine,
|
|
EndColumn: endCol,
|
|
Severity: ErrorSeverity[code],
|
|
})
|
|
}
|
|
|
|
// IsEmpty check if error exists
|
|
func (ve *ValidationError) IsEmpty() bool {
|
|
return len(ve.Errors) == 0
|
|
}
|