Add some exit codes

The external provider needs a simple way to indicate certain types of
errors. Duplicate error and not found error are such an example.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2023-03-26 22:31:55 +03:00
parent e7f208367b
commit 80e8f6dc1e
No known key found for this signature in database
GPG key ID: 7D073DCC2C074CB5
2 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,26 @@
package execution
import (
"errors"
gErrors "github.com/cloudbase/garm/errors"
)
const (
// ExitCodeNotFound is an exit code that indicates a Not Found error
ExitCodeNotFound int = 30
// ExitCodeDuplicate is an exit code that indicates a duplicate error
ExitCodeDuplicate int = 31
)
func ResolveErrorToExitCode(err error) int {
if err != nil {
if errors.Is(err, gErrors.ErrNotFound) {
return ExitCodeNotFound
} else if errors.Is(err, gErrors.ErrDuplicateEntity) {
return ExitCodeDuplicate
}
return 1
}
return 0
}

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"os/exec"
"github.com/cloudbase/garm/config"
garmErrors "github.com/cloudbase/garm/errors"
@ -107,7 +108,11 @@ func (e *external) DeleteInstance(ctx context.Context, instance string) error {
_, err := garmExec.Exec(ctx, e.execPath, nil, asEnv)
if err != nil {
return garmErrors.NewProviderError("provider binary %s returned error: %s", e.execPath, err)
var exitErr exec.ExitError
if !errors.As(err, &exitErr) || exitErr.ExitCode() != execution.ExitCodeNotFound {
return garmErrors.NewProviderError("provider binary %s returned error: %s", e.execPath, err)
}
}
return nil
}