70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package errors
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Exit codes
|
|
const (
|
|
ExitSuccess = 0
|
|
ExitGeneralError = 1
|
|
ExitConfigError = 2
|
|
ExitFileError = 3
|
|
ExitStorageError = 4
|
|
ExitNetworkError = 5
|
|
ExitInterrupted = 130
|
|
)
|
|
|
|
var (
|
|
// Config errors
|
|
ErrNoGlobalConfig = errors.New("no storage configured; run 'aevs config' first")
|
|
ErrNoProjectConfig = errors.New("no aevs.yaml found; run 'aevs init' first")
|
|
ErrConfigExists = errors.New("aevs.yaml already exists; use --force to overwrite")
|
|
ErrInvalidProject = errors.New("invalid project name; use only a-z, 0-9, -, _")
|
|
|
|
// Storage errors
|
|
ErrProjectNotFound = errors.New("project not found in storage")
|
|
ErrAccessDenied = errors.New("access denied; check your credentials")
|
|
ErrBucketNotFound = errors.New("bucket not found")
|
|
|
|
// File errors
|
|
ErrFileNotFound = errors.New("file not found")
|
|
ErrNoEnvFiles = errors.New("no env files found")
|
|
)
|
|
|
|
// GetExitCode returns the appropriate exit code for an error
|
|
func GetExitCode(err error) int {
|
|
if err == nil {
|
|
return ExitSuccess
|
|
}
|
|
|
|
// Check for specific errors
|
|
switch {
|
|
case errors.Is(err, ErrNoGlobalConfig),
|
|
errors.Is(err, ErrNoProjectConfig),
|
|
errors.Is(err, ErrConfigExists),
|
|
errors.Is(err, ErrInvalidProject):
|
|
return ExitConfigError
|
|
|
|
case errors.Is(err, ErrFileNotFound),
|
|
errors.Is(err, ErrNoEnvFiles),
|
|
os.IsNotExist(err):
|
|
return ExitFileError
|
|
|
|
case errors.Is(err, ErrProjectNotFound),
|
|
errors.Is(err, ErrAccessDenied),
|
|
errors.Is(err, ErrBucketNotFound):
|
|
return ExitStorageError
|
|
|
|
case strings.Contains(err.Error(), "connection"),
|
|
strings.Contains(err.Error(), "network"),
|
|
strings.Contains(err.Error(), "timeout"):
|
|
return ExitNetworkError
|
|
|
|
default:
|
|
return ExitGeneralError
|
|
}
|
|
}
|