Skip to content

Commit

Permalink
disable error handler instead of catch
Browse files Browse the repository at this point in the history
  • Loading branch information
brettmc committed Dec 19, 2024
1 parent bc5fbb9 commit bb75c6c
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/SDK/Resource/Detectors/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;
use function php_uname;
use Throwable;

/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host
Expand Down Expand Up @@ -48,25 +47,37 @@ private function getMachineId(): ?string
};
}

/**
* @phan-suppress PhanTypeMismatchArgumentInternal
*/
private function readFile(string $file): string|false
{
set_error_handler(static fn () => true);

try {
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);
if ($contents !== false) {
return $contents;
}
}
} finally {
restore_error_handler();
}

return false;
}

private function getLinuxId(): ?string
{
$paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID];

foreach ($paths as $path) {
$file = $this->dir . $path;

try {
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

if ($contents === false) {
continue;
}

return trim($contents);
}
} catch (Throwable $t) {
//do nothing
$contents = $this->readFile($file);
if ($contents !== false) {
return $contents;
}
}

Expand All @@ -77,16 +88,9 @@ private function getBsdId(): ?string
{
$file = $this->dir . self::PATH_ETC_HOSTID;

try {
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

if ($contents !== false) {
return trim($contents);
}
}
} catch (Throwable $t) {
//do nothing
$contents = $this->readFile($file);
if ($contents !== false) {
return $contents;
}

$out = exec('which kenv && kenv -q smbios.system.uuid');
Expand Down

0 comments on commit bb75c6c

Please sign in to comment.