Skip to content

Commit

Permalink
fix set_error_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMuller committed Jun 18, 2024
1 parent 19acfc8 commit 3d2ccfc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ doc/coverage
# wild
todo.md

.idea
9 changes: 7 additions & 2 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,23 @@ public function asVersion($version = null) {
public function set($code, $value) {
$this->_map[$code] = $value;
}
private function _unset($code) {

private function unset($code) {
unset($this->_map[$code]);
}

public function isEmpty() {
if (! is_countable($this->_map)) {
return true;
}

return count($this->_map) === 0;
}

public function __call($func, $args) {
switch ($func) {
case 'unset':
return $this->_unset($args[0]);
return $this->unset($args[0]);
break;
default:
throw new \BadMethodCallException(get_class($this) . "::$func() does not exist");
Expand Down
12 changes: 8 additions & 4 deletions src/RemoteLRS.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ protected function sendRequest($method, $resource) {

$success = false;

//
// errors from fopen are reported to PHP as E_WARNING which prevents us
// from getting a reasonable message, so set an error handler here for
// the immediate call to turn it into an exception, and then restore
// normal handling
//
set_error_handler(
function ($errno, $errstr, $errfile, $errline, array $errcontext) {
function ($errno, $errstr, $errfile, $errline) {
// "!== false" is intentional. strpos() can return 0, which is falsey, but returning
// 0 matches our "true" condition. Using strict equality to avoid that confusion.
if ($errno == E_NOTICE && strpos($errstr, 'Array to string conversion') !== false) {
Expand All @@ -162,7 +160,13 @@ function ($errno, $errstr, $errfile, $errline, array $errcontext) {
$fp = fopen($url, 'rb', false, $context);

if (! $fp) {
$content = "Request failed: $php_errormsg";
$content = "Request failed";

$last_error = error_get_last();

if($last_error && $last_error['type'] === E_ERROR) {
$content .= ": " . $last_error['message'];
}
}
}
catch (\ErrorException $ex) {
Expand Down

0 comments on commit 3d2ccfc

Please sign in to comment.