mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
change: add exit code
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
)
|
||||
|
||||
@@ -235,8 +236,6 @@ func InterfaceType(raw interface{}) string {
|
||||
return reflect.TypeOf(raw).String()
|
||||
}
|
||||
|
||||
var ErrUnsupportedFileExt = fmt.Errorf("unsupported file extension")
|
||||
|
||||
// LoadFile loads file content with file extension and assigns to structObj
|
||||
func LoadFile(path string, structObj interface{}) (err error) {
|
||||
log.Info().Str("path", path).Msg("load file")
|
||||
@@ -252,12 +251,15 @@ func LoadFile(path string, structObj interface{}) (err error) {
|
||||
decoder := json.NewDecoder(bytes.NewReader(file))
|
||||
decoder.UseNumber()
|
||||
err = decoder.Decode(structObj)
|
||||
err = errors.Wrap(code.LoadJSONError, err.Error())
|
||||
case ".yaml", ".yml":
|
||||
err = yaml.Unmarshal(file, structObj)
|
||||
err = errors.Wrap(code.LoadYAMLError, err.Error())
|
||||
case ".env":
|
||||
err = parseEnvContent(file, structObj)
|
||||
err = errors.Wrap(code.LoadEnvError, err.Error())
|
||||
default:
|
||||
err = ErrUnsupportedFileExt
|
||||
err = code.UnsupportedFileExtension
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -297,14 +299,14 @@ func loadFromCSV(path string) []map[string]interface{} {
|
||||
file, err := ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("read csv file failed")
|
||||
os.Exit(1)
|
||||
os.Exit(code.GetErrorCode(err))
|
||||
}
|
||||
|
||||
r := csv.NewReader(strings.NewReader(string(file)))
|
||||
content, err := r.ReadAll()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("parse csv file failed")
|
||||
os.Exit(1)
|
||||
os.Exit(code.GetErrorCode(err))
|
||||
}
|
||||
firstLine := content[0] // parameter names
|
||||
var result []map[string]interface{}
|
||||
@@ -323,7 +325,7 @@ func loadMessage(path string) []byte {
|
||||
file, err := ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("read message file failed")
|
||||
os.Exit(1)
|
||||
os.Exit(code.GetErrorCode(err))
|
||||
}
|
||||
return file
|
||||
}
|
||||
@@ -333,13 +335,13 @@ func ReadFile(path string) ([]byte, error) {
|
||||
path, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("convert absolute path failed")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(code.LoadFileError, err.Error())
|
||||
}
|
||||
|
||||
file, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("read file failed")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(code.LoadFileError, err.Error())
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
+43
-13
@@ -12,24 +12,40 @@ const (
|
||||
)
|
||||
|
||||
// environment: [2, 10)
|
||||
var (
|
||||
InvalidPython3Venv = errors.New("prepare python3 venv failed") // 9
|
||||
)
|
||||
|
||||
// loader: [10, 20)
|
||||
var (
|
||||
LoadError = errors.New("load error") // 10
|
||||
LoadJSONError = errors.New("load json error") // 11
|
||||
LoadYAMLError = errors.New("load yaml error") // 12
|
||||
LoadFileError = errors.New("load file error") // 10
|
||||
LoadJSONError = errors.New("load json error") // 11
|
||||
LoadYAMLError = errors.New("load yaml error") // 12
|
||||
LoadEnvError = errors.New("load .env error") // 13
|
||||
LoadCSVError = errors.New("load csv error") // 14
|
||||
InvalidCaseFormat = errors.New("invalid case format") // 15
|
||||
UnsupportedFileExtension = errors.New("unsupported file extension") // 16
|
||||
ReferencedFileNotFound = errors.New("referenced file not found") // 17
|
||||
InvalidPluginFile = errors.New("invalid plugin file") // 18
|
||||
)
|
||||
|
||||
// parser: [20, 30)
|
||||
var (
|
||||
ParseError = errors.New("parse error") // 20
|
||||
ParseStringError = errors.New("parse string failed") // 21
|
||||
ParseVariablesError = errors.New("parse variables failed") // 22
|
||||
ParseConfigError = errors.New("parse config error") // 25
|
||||
VariableNotFound = errors.New("variable not found") // 21
|
||||
ParseFunctionError = errors.New("parse function failed") // 22
|
||||
CallFunctionError = errors.New("call function failed") // 23
|
||||
ParseVariablesError = errors.New("parse variables failed") // 24
|
||||
)
|
||||
|
||||
// runner: [30, 40)
|
||||
|
||||
var (
|
||||
InitPluginFailed = errors.New("init plugin failed") // 31
|
||||
BuildGoPluginFailed = errors.New("build go plugin failed") // 32
|
||||
BuildPyPluginFailed = errors.New("build py plugin failed") // 33
|
||||
)
|
||||
|
||||
// summary: [40, 50)
|
||||
|
||||
// ios device related: [50, 60)
|
||||
@@ -67,16 +83,31 @@ var (
|
||||
// CV related: [90, 100)
|
||||
|
||||
var errorsMap = map[error]int{
|
||||
// environment
|
||||
InvalidPython3Venv: 9,
|
||||
|
||||
// loader
|
||||
LoadError: 10,
|
||||
LoadJSONError: 11,
|
||||
LoadYAMLError: 12,
|
||||
LoadFileError: 10,
|
||||
LoadJSONError: 11,
|
||||
LoadYAMLError: 12,
|
||||
LoadEnvError: 13,
|
||||
LoadCSVError: 14,
|
||||
InvalidCaseFormat: 15,
|
||||
UnsupportedFileExtension: 16,
|
||||
ReferencedFileNotFound: 17,
|
||||
InvalidPluginFile: 18,
|
||||
|
||||
// parser
|
||||
ParseError: 20,
|
||||
ParseStringError: 21,
|
||||
ParseVariablesError: 22,
|
||||
ParseConfigError: 25,
|
||||
VariableNotFound: 21,
|
||||
ParseFunctionError: 22,
|
||||
CallFunctionError: 23,
|
||||
ParseVariablesError: 24,
|
||||
|
||||
// runner
|
||||
InitPluginFailed: 31,
|
||||
BuildGoPluginFailed: 32,
|
||||
BuildPyPluginFailed: 33,
|
||||
|
||||
// ios related
|
||||
IOSDeviceConnectionError: 50,
|
||||
@@ -105,7 +136,6 @@ var errorsMap = map[error]int{
|
||||
|
||||
func GetErrorCode(err error) (exitCode int) {
|
||||
if err == nil {
|
||||
log.Info().Int("code", Success).Msg("hrp exit")
|
||||
return Success
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func EnsurePython3Venv(venv string, packages ...string) (python3 string, err err
|
||||
}
|
||||
python3, err = ensurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "prepare python3 venv failed")
|
||||
return "", errors.Wrap(code.InvalidPython3Venv, err.Error())
|
||||
}
|
||||
python3Executable = python3
|
||||
log.Info().Str("Python3Executable", python3Executable).Msg("set python3 executable path")
|
||||
|
||||
Reference in New Issue
Block a user