Skip to content

Commit

Permalink
json: Improve error handling
Browse files Browse the repository at this point in the history
The goal is to return an error as soon as a parsing error occurs.
The current code would sometimes ignore parsing errors and try to parse
as much as it can, which would give a partial VM representation, which
is not desirable.

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
  • Loading branch information
cfergeau authored and baude committed Jun 26, 2023
1 parent fb98993 commit 66d1e8c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/config/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func unmarshalBootloader(rawMsg json.RawMessage) (Bootloader, error) {
bootloader = &linux
}
default:
return nil, fmt.Errorf("unknown 'kind' field: '%s'", kind)
err = fmt.Errorf("unknown 'kind' field: '%s'", kind)
}

return bootloader, nil
return bootloader, err
}

func unmarshalDevices(rawMsg json.RawMessage) ([]VirtioDevice, error) {
Expand All @@ -71,15 +71,19 @@ func unmarshalDevices(rawMsg json.RawMessage) ([]VirtioDevice, error) {
)

err := json.Unmarshal(rawMsg, &rawDevices)
if err != nil {
return nil, err
}

for _, msg := range rawDevices {
dev, err := unmarshalDevice(*msg)
if err == nil {
devices = append(devices, dev)
if err != nil {
return nil, err
}
devices = append(devices, dev)
}

return devices, err
return devices, nil
}

func unmarshalDevice(rawMsg json.RawMessage) (VirtioDevice, error) {
Expand Down Expand Up @@ -129,7 +133,7 @@ func unmarshalDevice(rawMsg json.RawMessage) (VirtioDevice, error) {
err = json.Unmarshal(rawMsg, &newDevice)
dev = &newDevice
default:
return nil, fmt.Errorf("unknown 'kind' field: '%s'", kind)
err = fmt.Errorf("unknown 'kind' field: '%s'", kind)
}

if err != nil {
Expand Down

0 comments on commit 66d1e8c

Please sign in to comment.