Skip to content

Commit

Permalink
libmachine: use fmt.Errorf and errors.Wrap instead of custom errors
Browse files Browse the repository at this point in the history
Error type are not checked or used anywhere else.
The text in ErrHostDoesNotExist is also never displayed as we have an
other check before this one in client.Exist():
`Machine 'crc' does not exist. Use 'crc start' to create it`
  • Loading branch information
guillaumerose authored and praveenkumar committed Feb 1, 2021
1 parent ef38816 commit 90a9404
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 46 deletions.
8 changes: 3 additions & 5 deletions pkg/libmachine/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package host

import (
"errors"
"fmt"
"net/rpc"
"regexp"
"strings"

log "github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/libmachine/mcnerror"
"github.com/code-ready/crc/pkg/libmachine/mcnutils"
"github.com/code-ready/machine/libmachine/drivers"
"github.com/code-ready/machine/libmachine/state"
Expand Down Expand Up @@ -35,10 +36,7 @@ func ValidateHostName(name string) bool {

func (h *Host) runActionForState(action func() error, desiredState state.State) error {
if drivers.MachineInState(h.Driver, desiredState)() {
return mcnerror.ErrHostAlreadyInState{
Name: h.Name,
State: desiredState,
}
return fmt.Errorf("machine %q is already %s", h.Name, strings.ToLower(desiredState.String()))
}

if err := action(); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/libmachine/libmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
log "github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/drivers/hyperv"
"github.com/code-ready/crc/pkg/libmachine/host"
"github.com/code-ready/crc/pkg/libmachine/mcnerror"
"github.com/code-ready/crc/pkg/libmachine/mcnutils"
"github.com/code-ready/crc/pkg/libmachine/persist"
"github.com/code-ready/crc/pkg/libmachine/version"
"github.com/code-ready/machine/libmachine/drivers"
rpcdriver "github.com/code-ready/machine/libmachine/drivers/rpc"
"github.com/code-ready/machine/libmachine/state"
"github.com/pkg/errors"
)

type API interface {
Expand Down Expand Up @@ -91,9 +91,7 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Running pre-create checks...")

if err := h.Driver.PreCreateCheck(); err != nil {
return mcnerror.ErrDuringPreCreate{
Cause: err,
}
return errors.Wrap(err, "error with pre-create check")
}

if err := api.Save(h); err != nil {
Expand Down
33 changes: 0 additions & 33 deletions pkg/libmachine/mcnerror/errors.go

This file was deleted.

5 changes: 1 addition & 4 deletions pkg/libmachine/persist/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

log "github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/libmachine/host"
"github.com/code-ready/crc/pkg/libmachine/mcnerror"
)

type Filestore struct {
Expand Down Expand Up @@ -104,9 +103,7 @@ func (s Filestore) Load(name string) (*host.Host, error) {
hostPath := filepath.Join(s.GetMachinesDir(), name)

if _, err := os.Stat(hostPath); os.IsNotExist(err) {
return nil, mcnerror.ErrHostDoesNotExist{
Name: name,
}
return nil, fmt.Errorf("machine %s does not exist", name)
}
data, err := ioutil.ReadFile(filepath.Join(s.GetMachinesDir(), name, "config.json"))
if err != nil {
Expand Down

0 comments on commit 90a9404

Please sign in to comment.