Merge branch 'session_refactor' into 'master'

feat: 优化driver层错误码返回

See merge request iesqa/httprunner!127
This commit is contained in:
李隆
2025-07-15 16:44:12 +00:00
6 changed files with 18 additions and 14 deletions

2
go.mod
View File

@@ -24,6 +24,7 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/go-openapi/spec v0.20.7
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/httprunner/funplugin v0.5.5
github.com/jinzhu/copier v0.3.5
@@ -82,7 +83,6 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/grandcat/zeroconf v1.0.0 // indirect

View File

@@ -1 +1 @@
v5.0.0-250711
v5.0.0-250716

View File

@@ -14,10 +14,12 @@ import (
"strings"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/json"
"github.com/httprunner/httprunner/v5/uixt/option"
)
@@ -160,7 +162,7 @@ func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.Act
var bsJSON []byte = nil
if data != nil {
if bsJSON, err = json.Marshal(data); err != nil {
return nil, err
return nil, errors.Wrap(code.InvalidParamError, err.Error())
}
}
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
@@ -191,7 +193,8 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [
return rawResp, nil
}
lastError = err
// Notice: use DeviceHTTPDriverError when request driver failed
lastError = errors.Wrap(code.DeviceHTTPDriverError, err.Error())
log.Warn().Err(err).Msgf("request failed, attempt %d/%d", attempt, s.maxRetry)
// If this was the last attempt, break
@@ -220,6 +223,7 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
rawResp DriverRawResponse, err error,
) {
logid := uuid.New().String()
timeout := s.timeout
options := option.NewActionOptions(opts...)
if options.Timeout > 0 {
@@ -251,7 +255,7 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, op
logger = log.Debug().Bool("success", true)
}
logger = logger.Str("request_method", method).Str("request_url", rawURL).
logger = logger.Str("logid", logid).Str("request_method", method).Str("request_url", rawURL).
Str("request_body", string(rawBody))
if !driverResult.RequestTime.IsZero() {
logger = logger.Int64("request_time", driverResult.RequestTime.UnixMilli())
@@ -274,6 +278,8 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, op
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Accept", "application/json")
req.Header.Set("X-HTTP-Request-ID", logid)
req.Header.Set("logid", logid)
driverResult.RequestTime = time.Now()
var resp *http.Response

View File

@@ -231,8 +231,7 @@ func (dev *IOSDevice) NewDriver() (driver IDriver, err error) {
if dev.Options.ResetHomeOnStartup {
log.Info().Msg("go back to home screen")
if err = wdaDriver.Home(); err != nil {
return nil, errors.Wrap(code.MobileUIDriverError,
fmt.Sprintf("go back to home screen failed: %v", err))
return nil, errors.Wrap(err, "go back to home screen failed")
}
}
if dev.Options.LogOn {

View File

@@ -268,7 +268,7 @@ func (wd *WDADriver) Scale() (float64, error) {
}
screen, err := wd.Screen()
if err != nil {
return 0, errors.Wrap(code.MobileUIDriverError,
return 0, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("get screen info failed: %v", err))
}
return screen.Scale, nil
@@ -454,8 +454,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
}
_, err = wd.Session.POST(data, "/wings/apps/launch")
if err != nil {
return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launch failed: %v", err))
return errors.Wrap(err, "wda app launch failed")
}
return nil
}
@@ -466,8 +465,7 @@ func (wd *WDADriver) AppLaunchUnattached(bundleId string) (err error) {
data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.Session.POST(data, "/wda/apps/launchUnattached")
if err != nil {
return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launchUnattached failed: %v", err))
return errors.Wrap(err, "wda app launchUnattached failed")
}
return nil
}

View File

@@ -151,7 +151,8 @@ func (dExt *XTDriver) ExecuteAction(ctx context.Context, action option.MobileAct
// Execute via MCP tool
result, err := dExt.client.CallTool(ctx, req)
if err != nil {
return SessionData{}, fmt.Errorf("MCP tool call failed: %w", err)
// Notice: preserve the original error code
return SessionData{}, errors.Wrap(err, "call MCP tool failed")
}
// Check if the tool execution had business logic errors
@@ -259,7 +260,7 @@ func (dExt *XTDriver) CallMCPTool(ctx context.Context,
Str("server", serverName).
Str("tool", toolName).
Msg("call MCP tool failed")
return nil, err
return nil, errors.Wrap(err, "call MCP tool failed")
}
if result.IsError {