Skip to content

Commit

Permalink
Improve exception messages
Browse files Browse the repository at this point in the history
If possible, use the last error produced by file_get_contents
as an exception message
  • Loading branch information
marein committed Nov 9, 2020
1 parent f19151e commit d9238bc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/HttpAdapter/HttpStreamWrapperClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,20 @@ private function request(string $method, Request $request): Response
$context = stream_context_create($options);

// Suppress errors for file_get_contents. We will analyze this ourselves.
set_error_handler(fn() => true);
$errorReportingLevelBeforeFileGetContents = error_reporting(0);

$responseBody = file_get_contents(
$url,
false,
$context
);

restore_error_handler();
error_reporting($errorReportingLevelBeforeFileGetContents);

if ($responseBody === false) {
throw new NchanException('Unable to connect to ' . $url . '.');
throw new NchanException(
error_get_last()['message'] ?? 'Unable to connect to ' . $url . '.'
);
}

return HttpStreamWrapperResponse::fromResponse($http_response_header, $responseBody);
Expand Down

0 comments on commit d9238bc

Please sign in to comment.