refactor: fix exception handling (#2835)

* refactor: fix exception handling

The removed catch is never uses in php versions above 7.
The need for multiple catch statements like this is to support both php 5 and 7.

* remove traces of old exception handling

* add typehints

* dont treat exception code 0 specially
This commit is contained in:
Dag 2022-06-22 18:30:06 +02:00 committed by GitHub
parent 07927008eb
commit fad0dbb6ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 82 deletions

View File

@ -153,7 +153,7 @@ class DisplayAction extends ActionAbstract {
'donationUri' => $bridge->getDonationURI(), 'donationUri' => $bridge->getDonationURI(),
'icon' => $bridge->getIcon() 'icon' => $bridge->getIcon()
); );
} catch(Error $e) { } catch(\Throwable $e) {
error_log($e); error_log($e);
if(logBridgeError($bridge::NAME, $e->getCode()) >= Configuration::getConfig('error', 'report_limit')) { if(logBridgeError($bridge::NAME, $e->getCode()) >= Configuration::getConfig('error', 'report_limit')) {
@ -163,22 +163,12 @@ class DisplayAction extends ActionAbstract {
// Create "new" error message every 24 hours // Create "new" error message every 24 hours
$this->userData['_error_time'] = urlencode((int)(time() / 86400)); $this->userData['_error_time'] = urlencode((int)(time() / 86400));
// Error 0 is a special case (i.e. "trying to get property of non-object") $message = sprintf(
if($e->getCode() === 0) { 'Bridge returned error %s! (%s)',
$item->setTitle( $e->getCode(),
'Bridge encountered an unexpected situation! (' $this->userData['_error_time']
. $this->userData['_error_time'] );
. ')' $item->setTitle($message);
);
} else {
$item->setTitle(
'Bridge returned error '
. $e->getCode()
. '! ('
. $this->userData['_error_time']
. ')'
);
}
$item->setURI( $item->setURI(
(isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '') (isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '')
@ -189,38 +179,6 @@ class DisplayAction extends ActionAbstract {
$item->setTimestamp(time()); $item->setTimestamp(time());
$item->setContent(buildBridgeException($e, $bridge)); $item->setContent(buildBridgeException($e, $bridge));
$items[] = $item;
} elseif(Configuration::getConfig('error', 'output') === 'http') {
header('Content-Type: text/html', true, $this->get_return_code($e));
die(buildTransformException($e, $bridge));
}
}
} catch(Exception $e) {
error_log($e);
if(logBridgeError($bridge::NAME, $e->getCode()) >= Configuration::getConfig('error', 'report_limit')) {
if(Configuration::getConfig('error', 'output') === 'feed') {
$item = new \FeedItem();
// Create "new" error message every 24 hours
$this->userData['_error_time'] = urlencode((int)(time() / 86400));
$item->setURI(
(isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '')
. '?'
. http_build_query($this->userData)
);
$item->setTitle(
'Bridge returned error '
. $e->getCode()
. '! ('
. $this->userData['_error_time']
. ')'
);
$item->setTimestamp(time());
$item->setContent(buildBridgeException($e, $bridge));
$items[] = $item; $items[] = $item;
} elseif(Configuration::getConfig('error', 'output') === 'http') { } elseif(Configuration::getConfig('error', 'output') === 'http') {
header('Content-Type: text/html', true, $this->get_return_code($e)); header('Content-Type: text/html', true, $this->get_return_code($e));
@ -251,11 +209,7 @@ class DisplayAction extends ActionAbstract {
header('Content-Type: ' . $format->getMimeType() . '; charset=' . $format->getCharset()); header('Content-Type: ' . $format->getMimeType() . '; charset=' . $format->getCharset());
echo $format->stringify(); echo $format->stringify();
} catch(Error $e) { } catch(\Throwable $e) {
error_log($e);
header('Content-Type: text/html', true, $e->getCode());
die(buildTransformException($e, $bridge));
} catch(Exception $e) {
error_log($e); error_log($e);
header('Content-Type: text/html', true, $e->getCode()); header('Content-Type: text/html', true, $e->getCode());
die(buildTransformException($e, $bridge)); die(buildTransformException($e, $bridge));

View File

@ -66,20 +66,8 @@ function buildGitHubIssueQuery($title, $body, $labels = null, $maintainer = null
return $uri; return $uri;
} }
/** function buildBridgeException(\Throwable $e, BridgeInterface $bridge): string
* Returns the exception message as HTML string {
*
* @param object $e Exception The exception to show
* @param object $bridge object The bridge object
* @return string|null Returns the exception as HTML string or null.
*
* @todo This function belongs inside a class
*/
function buildBridgeException($e, $bridge){
if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
return null;
}
$title = $bridge->getName() . ' failed with error ' . $e->getCode(); $title = $bridge->getName() . ' failed with error ' . $e->getCode();
// Build a GitHub compatible message // Build a GitHub compatible message
@ -106,20 +94,8 @@ EOD;
return $section; return $section;
} }
/** function buildTransformException(\Throwable $e, BridgeInterface $bridge): string
* Returns the exception message as HTML string {
*
* @param object $e Exception The exception to show
* @param object $bridge object The bridge object
* @return string|null Returns the exception as HTML string or null.
*
* @todo This function belongs inside a class
*/
function buildTransformException($e, $bridge){
if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
return null;
}
$title = $bridge->getName() . ' failed with error ' . $e->getCode(); $title = $bridge->getName() . ' failed with error ' . $e->getCode();
// Build a GitHub compatible message // Build a GitHub compatible message