Skip to content

Commit

Permalink
GuzzleMiddleware issue with Guzzle\Promise\Create::rejectionFor()
Browse files Browse the repository at this point in the history
Additional file-size & complexity reduction
HttpMessage/Utility/Uri::parseUrl now accepts a UriInterface (why not)
Teams - can now pass a UriInterface as $url param
  • Loading branch information
bkdotcom committed Jan 17, 2024
1 parent 07e88a0 commit 33885d4
Show file tree
Hide file tree
Showing 33 changed files with 789 additions and 668 deletions.
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<exclude name="Generic.Commenting.DocComment.LongNotCapital" />
<exclude name="Generic.Commenting.DocComment.MissingShort" />
<exclude name="Generic.Commenting.DocComment.ShortNotCapital" />
<exclude name="Generic.Commenting.DocComment.SpacingAfterTagGroup" />
<exclude name="Generic.Files.EndFileNewline.NotFound" /> <!-- handled by PSR-12 -->
<exclude name="Generic.Files.EndFileNoNewline.Found" />
<exclude name="Generic.Files.InlineHTML.Found" />
Expand Down Expand Up @@ -139,6 +140,7 @@
<!-- VariableComment -->
<exclude name="Squiz.Commenting.VariableComment.IncorrectVarType" />
<exclude name="Squiz.Commenting.VariableComment.Missing" />
<exclude name="Squiz.Commenting.VariableComment.TagNotAllowed" />
<exclude name="Squiz.Commenting.VariableComment.WrongStyle" />
</rule>
<rule ref="Squiz.Commenting.FileComment.Missing">
Expand Down
6 changes: 4 additions & 2 deletions src/Debug/Collector/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @package PHPDebugConsole
* @author Brad Kent <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2022 Brad Kent
* @copyright 2014-2024 Brad Kent
* @version v3.0
*/

Expand Down Expand Up @@ -96,7 +96,9 @@ public function onRejected(GuzzleException $reason, array $requestInfo)
}
$this->logResponse($response, $requestInfo, $reason);
$this->debug->groupEnd($meta);
return Promise\Create::rejectionFor($reason);
return \class_exists('GuzzleHttp\\Promise\\Create')
? Promise\Create::rejectionFor($reason)
: Promise\rejection_for($reason);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Debug/Framework/Yii1_1/UserInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use CModel;
use CWebApplication;
use Exception;
use IWebUser;
use Yii;

/**
Expand Down Expand Up @@ -69,12 +68,12 @@ public function log()
/**
* Log user attributes
*
* @param IWebUser $user User instance
* @param Debug $debug Debug instance
* @param CApplicationComponent $user User instance (web or console)
* @param Debug $debug Debug instance
*
* @return void
*/
private function logIdentityData(IWebUser $user, Debug $debug)
private function logIdentityData(CApplicationComponent $user, Debug $debug)
{
$identityData = $user->model->attributes;
if ($user->model instanceof CModel) {
Expand Down
122 changes: 122 additions & 0 deletions src/Debug/Route/AbstractErrorRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v3.3
*/

namespace bdk\Debug\Route;

use bdk\Debug;
use bdk\ErrorHandler;
use bdk\ErrorHandler\Error;

/**
* common "shouldSend" method
*/
abstract class AbstractErrorRoute extends AbstractRoute
{
protected $statsKey = '';

/**
* Constructor
*
* @param Debug $debug debug instance
*/
public function __construct(Debug $debug)
{
parent::__construct($debug);
$this->cfg = \array_merge($this->cfg, array(
'errorMask' => E_ERROR | E_PARSE | E_COMPILE_ERROR | E_WARNING | E_USER_ERROR,
));
$debug->errorHandler->setCfg('enableStats', true);
}

/**
* {@inheritDoc}
*/
public function getSubscriptions()
{
return array(
ErrorHandler::EVENT_ERROR => array('onError', -1),
);
}

/**
* ErrorHandler::EVENT_ERROR event subscriber
*
* @param Error $error error/event object
*
* @return void
*/
public function onError(Error $error)
{
if ($this->shouldSend($error, $this->statsKey) === false) {
return;
}
$messages = $this->buildMessages($error);
$this->sendMessages($messages);
}

/**
* Build messages to send to client
*
* @param Error $error Error instance
*
* @return array
*/
abstract protected function buildMessages(Error $error);

/**
* Send messages to client (ie Discord, Slack, or Teams)
*
* @param array $messages array of message(s) to send to client
*
* @return void
*/
abstract protected function sendMessages(array $messages);

/**
* Should we send a notification for this error?
*
* @param Error $error Error instance
* @param string $statsKey name under which we store stats
*
* @return bool
*/
private function shouldSend(Error $error, $statsKey)
{
if ($error['throw']) {
// subscriber that set throw *should have* stopped error propagation
return false;
}
if (($error['type'] & $this->cfg['errorMask']) !== $error['type']) {
return false;
}
if ($error['isFirstOccur'] === false) {
return false;
}
if ($error['inConsole']) {
return false;
}
$error['stats'] = \array_merge(array(
$statsKey => array(
'countSince' => 0,
'timestamp' => null,
),
), $error['stats'] ?: array());
$tsCutoff = \time() - $this->cfg['throttleMin'] * 60;
if ($error['stats'][$statsKey]['timestamp'] > $tsCutoff) {
// This error was recently sent
$error['stats'][$statsKey]['countSince']++;
return false;
}
$error['stats'][$statsKey]['timestamp'] = \time();
return true;
}
}
62 changes: 19 additions & 43 deletions src/Debug/Route/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use bdk\CurlHttpMessage\Client as CurlHttpMessageClient;
use bdk\Debug;
use bdk\ErrorHandler;
use bdk\ErrorHandler\Error;
use RuntimeException;

Expand All @@ -25,10 +24,8 @@
*
* @see https://discord.com/developers/docs/resources/webhook#execute-webhook
*/
class Discord extends AbstractRoute
class Discord extends AbstractErrorRoute
{
use ErrorThrottleTrait;

protected $cfg = array(
'errorMask' => 0,
'onClientInit' => null,
Expand All @@ -39,45 +36,17 @@ class Discord extends AbstractRoute
/** @var CurlHttpMessageClient */
protected $client;

protected $statsKey = 'discord';

/**
* Constructor
*
* @param Debug $debug debug instance
* {@inheritDoc}
*/
public function __construct(Debug $debug)
{
parent::__construct($debug);
$this->cfg = \array_merge($this->cfg, array(
'errorMask' => E_ERROR | E_PARSE | E_COMPILE_ERROR | E_WARNING | E_USER_ERROR,
'webhookUrl' => \getenv('DISCORD_WEBHOOK_URL'),
));
$debug->errorHandler->setCfg('enableStats', true);
}

/**
* {@inheritDoc}
*/
public function getSubscriptions()
{
return array(
ErrorHandler::EVENT_ERROR => array('onError', -1),
);
}

/**
* ErrorHandler::EVENT_ERROR event subscriber
*
* @param Error $error error/event object
*
* @return void
*/
public function onError(Error $error)
{
if ($this->shouldSend($error, 'discord') === false) {
return;
}
$message = $this->buildMessage($error);
$this->sendMessage($message);
}

/**
Expand All @@ -101,23 +70,20 @@ private function assertCfg()
}

/**
* Build Discord error message(s)
*
* @param Error $error Error instance
*
* @return array
* {@inheritDoc}
*/
private function buildMessage(Error $error)
protected function buildMessages(Error $error)
{
$emoji = $error->isFatal()
? ':no_entry:'
: ':warning:';
return array(
$message = array(
'content' => $emoji . ' **' . $error['typeStr'] . '**' . "\n"
. $this->getRequestMethodUri() . "\n"
. $error->getMessageText() . "\n"
. $error['fileAndLine'],
);
return array($message);
}

/**
Expand All @@ -139,7 +105,17 @@ protected function getClient()
}

/**
* Send message to Discord
* {@inheritDoc}
*/
protected function sendMessages(array $messages)
{
foreach ($messages as $message) {
$this->sendMessage($message);
}
}

/**
* Send message
*
* @param array $message Discord message
*
Expand Down
60 changes: 0 additions & 60 deletions src/Debug/Route/ErrorThrottleTrait.php

This file was deleted.

Loading

0 comments on commit 33885d4

Please sign in to comment.