• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 4702166895

pending completion
4702166895

push

github

twinh
feat(plugin): 增加 `httpComplete` 事件,在 HTTP 请求完成时触发

0 of 1 new or added line in 1 file covered. (0.0%)

9 existing lines in 1 file now uncovered.

855 of 2262 relevant lines covered (37.8%)

18.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Callback/HttpCallback.php
1
<?php
2

3
namespace Miaoxing\Plugin\Callback;
4

5
use Wei\Event;
6
use Wei\Http;
7

8
abstract class HttpCallback
9
{
10
    public const MAX_LOG_LENGTH = 1024;
11

12
    /**
13
     * @param mixed $response
14
     * @param Http $http
15
     */
16
    public static function success($response, Http $http)
17
    {
UNCOV
18
        $retries = $http->getOption('retries');
×
UNCOV
19
        $leftRetries = $http->getOption('leftRetries');
×
20

21
        if ($retries && $retries != $leftRetries) {
×
22
            // 上报是第几次重试才成功
UNCOV
23
            $curRetry = $retries - $leftRetries;
×
24
            wei()->statsD->increment('http.retries.' . $curRetry . '.success');
×
25
        }
26
    }
27

28
    /**
29
     * cURL请求发送失败的回调,记录错误原因和异常堆栈
30
     *
31
     * @param Http $http
32
     */
33
    public static function error(Http $http)
34
    {
35
        // 只有重试了仍然错误才记录日志
UNCOV
36
        if ($http->getOption('leftRetries')) {
×
UNCOV
37
            return;
×
38
        }
39

40
        $level = $http->getOption('errorLevel') ?: 'error';
×
41

UNCOV
42
        $response = $http->getResponseText();
×
43
        if (mb_strlen($response) > static::MAX_LOG_LENGTH) {
×
UNCOV
44
            $body = mb_substr($response, 0, static::MAX_LOG_LENGTH) . '...';
×
45
        } else {
46
            $body = $response;
×
47
        }
48

49
        $exception = $http->getErrorException();
×
UNCOV
50
        wei()->logger->log($level, $exception->getMessage(), [
×
UNCOV
51
            'url' => $http->getUrl(),
×
52
            'method' => $http->getMethod(),
×
53
            'data' => $http->getData(),
×
54
            'curlInfo' => $http->getCurlInfo(),
×
55
            'curlOptions' => $http->getOption('curlOptions'),
×
56
            'responseBody' => $body,
×
57
            'code' => $exception->getCode(),
×
58
            'exception' => (string) $exception,
×
59
        ]);
×
60
    }
61

62
    public static function complete(Http $http)
63
    {
NEW
64
        Event::trigger('httpComplete', $http);
×
65
    }
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc