add custom error to be able to ignore or fatalF when reference is missing in collection
This commit is contained in:
parent
1b54bbb909
commit
34a6156097
8
pkg/cwhub/errors.go
Normal file
8
pkg/cwhub/errors.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package cwhub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*To be used when reference(s) (is/are) missing in a collection*/
|
||||||
|
var ReferenceMissingError = errors.New("Reference(s) missing in collection")
|
|
@ -3,6 +3,7 @@ package cwhub
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -375,7 +376,9 @@ func GetHubIdx() error {
|
||||||
}
|
}
|
||||||
ret, err := LoadPkgIndex(bidx)
|
ret, err := LoadPkgIndex(bidx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to load existing index : %v.", err)
|
if !errors.Is(err, ReferenceMissingError) {
|
||||||
|
log.Fatalf("Unable to load existing index : %v.", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
HubIdx = ret
|
HubIdx = ret
|
||||||
if err := LocalSync(); err != nil {
|
if err := LocalSync(); err != nil {
|
||||||
|
@ -391,7 +394,9 @@ func UpdateHubIdx() error {
|
||||||
}
|
}
|
||||||
ret, err := LoadPkgIndex(bidx)
|
ret, err := LoadPkgIndex(bidx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to load freshly downloaded index : %v.", err)
|
if !errors.Is(err, ReferenceMissingError) {
|
||||||
|
log.Fatalf("Unable to load freshly downloaded index : %v.", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
HubIdx = ret
|
HubIdx = ret
|
||||||
if err := LocalSync(); err != nil {
|
if err := LocalSync(); err != nil {
|
||||||
|
@ -450,6 +455,7 @@ func DisplaySummary() {
|
||||||
func LoadPkgIndex(buff []byte) (map[string]map[string]Item, error) {
|
func LoadPkgIndex(buff []byte) (map[string]map[string]Item, error) {
|
||||||
var err error
|
var err error
|
||||||
var RawIndex map[string]map[string]Item
|
var RawIndex map[string]map[string]Item
|
||||||
|
var missingItems []string
|
||||||
|
|
||||||
if err = json.Unmarshal(buff, &RawIndex); err != nil {
|
if err = json.Unmarshal(buff, &RawIndex); err != nil {
|
||||||
return nil, fmt.Errorf("failed to unmarshal index : %v", err)
|
return nil, fmt.Errorf("failed to unmarshal index : %v", err)
|
||||||
|
@ -473,12 +479,16 @@ func LoadPkgIndex(buff []byte) (map[string]map[string]Item, error) {
|
||||||
for _, p := range ptr {
|
for _, p := range ptr {
|
||||||
if _, ok := RawIndex[ptrtype][p]; !ok {
|
if _, ok := RawIndex[ptrtype][p]; !ok {
|
||||||
log.Errorf("Referred %s %s in collection %s doesn't exist.", ptrtype, p, item.Name)
|
log.Errorf("Referred %s %s in collection %s doesn't exist.", ptrtype, p, item.Name)
|
||||||
|
missingItems = append(missingItems, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(missingItems) > 0 {
|
||||||
|
return RawIndex, fmt.Errorf("%q : %w", missingItems, ReferenceMissingError)
|
||||||
|
}
|
||||||
|
|
||||||
return RawIndex, nil
|
return RawIndex, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue