Created
May 2, 2014 15:05
-
-
Save jdpedrie/ea32c2f4a94c32092e52 to your computer and use it in GitHub Desktop.
Turn WP_Error into Catchable Exception.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use WP_Error; | |
use App\Exceptions\RemoteCallException; | |
function remote_get($url, $args) | |
{ | |
$result = wp_remote_get($url, $args); | |
if ($result instanceOf WP_Error) { | |
$errorCode = current(array_keys($result->errors)); | |
$errorMsg = $result->errors[$errorCode][0]; | |
throw new RemoteCallException($errorCode .': '. $errorMsg); | |
} | |
return $result; | |
} | |
try { | |
$res = remote_get($url, $args); | |
} | |
catch (RemoteCallException $e) { | |
echo $e->getMessage(); // http_request_failed: A valid URL was not provided. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment