mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Fill typed messages for pmd loading
This commit is contained in:
parent
d9fe7488d3
commit
dd15eea48e
2 changed files with 37 additions and 8 deletions
|
|
@ -23,6 +23,25 @@ type ProviderMetadataLoader struct {
|
||||||
logging func(string, ...any)
|
logging func(string, ...any)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProviderMetadataLoadMessageType is the type of the message.
|
||||||
|
type ProviderMetadataLoadMessageType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
//JSONDecodingFailed indicates problems with JSON decoding
|
||||||
|
JSONDecodingFailed ProviderMetadataLoadMessageType = iota
|
||||||
|
// SchemaValidationFailed indicates a general problem with schema validation.
|
||||||
|
SchemaValidationFailed
|
||||||
|
// SchemaValidationFailedDetail is a failure detail in schema validation.
|
||||||
|
SchemaValidationFailedDetail
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProviderMetadataLoadMessage is a message generated while loading
|
||||||
|
// a provider meta data file.
|
||||||
|
type ProviderMetadataLoadMessage struct {
|
||||||
|
Type ProviderMetadataLoadMessageType
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
// LoadedProviderMetadata represents a loaded provider metadata.
|
// LoadedProviderMetadata represents a loaded provider metadata.
|
||||||
type LoadedProviderMetadata struct {
|
type LoadedProviderMetadata struct {
|
||||||
// URL is location where the document was found.
|
// URL is location where the document was found.
|
||||||
|
|
@ -32,7 +51,7 @@ type LoadedProviderMetadata struct {
|
||||||
// Hash is a SHA256 sum over the document.
|
// Hash is a SHA256 sum over the document.
|
||||||
Hash []byte
|
Hash []byte
|
||||||
// Messages are the error message happened while loading.
|
// Messages are the error message happened while loading.
|
||||||
Messages []string
|
Messages []ProviderMetadataLoadMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid returns true if the loaded document is valid.
|
// Valid returns true if the loaded document is valid.
|
||||||
|
|
|
||||||
24
csaf/util.go
24
csaf/util.go
|
|
@ -93,21 +93,31 @@ func loadProviderMetadataFromURL(
|
||||||
|
|
||||||
// We have loaded it the first time.
|
// We have loaded it the first time.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Messages = []string{fmt.Sprintf("%s: Decoding JSON failed: %v", url, err)}
|
result.Messages = []ProviderMetadataLoadMessage{{
|
||||||
|
Type: JSONDecodingFailed,
|
||||||
|
Message: fmt.Sprintf("%s: Decoding JSON failed: %v", url, err),
|
||||||
|
}}
|
||||||
storeLoaded()
|
storeLoaded()
|
||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|
||||||
switch errors, err := ValidateProviderMetadata(doc); {
|
switch errors, err := ValidateProviderMetadata(doc); {
|
||||||
case err != nil:
|
case err != nil:
|
||||||
result.Messages = []string{
|
result.Messages = []ProviderMetadataLoadMessage{{
|
||||||
fmt.Sprintf("%s: Validating against JSON schema failed: %v", url, err)}
|
Type: SchemaValidationFailed,
|
||||||
|
Message: fmt.Sprintf("%s: Validating against JSON schema failed: %v", url, err),
|
||||||
|
}}
|
||||||
|
|
||||||
case len(errors) > 0:
|
case len(errors) > 0:
|
||||||
result.Messages = []string{
|
result.Messages = []ProviderMetadataLoadMessage{{
|
||||||
fmt.Sprintf("%s: Validating against JSON schema failed: %v", url, err)}
|
Type: SchemaValidationFailed,
|
||||||
|
Message: fmt.Sprintf("%s: Validating against JSON schema failed: %v", url, err),
|
||||||
|
}}
|
||||||
for _, msg := range errors {
|
for _, msg := range errors {
|
||||||
result.Messages = append(result.Messages, strings.ReplaceAll(msg, `%`, `%%`))
|
result.Messages = append(result.Messages, ProviderMetadataLoadMessage{
|
||||||
|
Type: SchemaValidationFailedDetail,
|
||||||
|
Message: strings.ReplaceAll(msg, `%`, `%%`),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// Only store in result if validation passed.
|
// Only store in result if validation passed.
|
||||||
|
|
@ -193,7 +203,7 @@ func LoadProviderMetadataForDomain(
|
||||||
}
|
}
|
||||||
alreadyLogged[result] = url
|
alreadyLogged[result] = url
|
||||||
for _, msg := range result.Messages {
|
for _, msg := range result.Messages {
|
||||||
logging(msg)
|
logging(msg.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue