Skip to content

Instantly share code, notes, and snippets.

@jdpedrie
Created May 2, 2014 15:05
Show Gist options
  • Save jdpedrie/ea32c2f4a94c32092e52 to your computer and use it in GitHub Desktop.
Save jdpedrie/ea32c2f4a94c32092e52 to your computer and use it in GitHub Desktop.
Turn WP_Error into Catchable Exception.
<?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