I am trying to access the web service. The below provided code gives the exception message: "Bad Request". Is there any way to see what exactly is wrong in my request? How to debug this code in order to see which parameters in $p are not correct?
try
{
$webServices = $client->queryTest($p);
}
catch (Exception $e) {
print $e->getMessage();
}
Related
I'm trying to ignore a PHP error via a try catch block but it doesn't seem to be working? I'm using it inside of of my controllers in Laravel.
try {
if (!self::isEmulatorOnline()) {
return;
}
$MUSdata = $command . chr(1) . $data;
$socket = \socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
$connect_timeval = array(
"sec"=>0,
"usec" => 100
);
\socket_set_option(
$socket,
SOL_SOCKET,
SO_SNDTIMEO,
$connect_timeval
);
\socket_connect($socket, Config::get('frontend.client_host_ip'), Config::get('frontend.mus_host_port'));
\socket_send($socket, $MUSdata, strlen($MUSdata), MSG_DONTROUTE);
\socket_close($socket);
}
catch(\PHPException $exception) {}
As you can see I am trying to silent the error exception, I know it is advised not to but its via an ajax request where I handle if the client IP and port can't be accessed using a different method.
Does anyone know why its returning the error exception even when making it silent out using try catch?
The error I am getting is
1/1) ErrorException
socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it.
On this line:
\socket_connect($socket, Config::get('frontend.client_host_ip'), Config::get('frontend.mus_host_port'));
You're trying to catch the wrong exception. socket_connect() throws an ErrorException and not an PHPException. You can also specify just the Exception class to catch all unhandled exceptions.
You can also add multiple catch blocks if you want to catch multiple exception classes to handle them differently.
Example:
try {
//
} catch (ErrorException $ex) {
// here you go.
}
In laravel you can catch error like this.
Try logging this
try {
//
} catch (\Exception $e) {
// catch error message
Log::info($e->getMessage());
// get http error code
Log::info($e->getCode());
}
I'm trying to customize error messages.
For handling errors i used "try/catch" block according to this Recurly documentation, like this for example:
try {
$account = Recurly_Account::get('my_account_id');
$subscription = new Recurly_Subscription();
$subscription->account = $account;
$subscription->plan_code = 'my_plan_code';
$subscription->coupon_code = 'my_coupon_code';
/* .. etc .. */
$subscription->create();
}
catch (Exception $e) {
$errorMsg = $e->getMessage();
print $errorMsg;
}
I wanted use code in catch block like this:
catch (Exception $e) {
$errorCode = $e->getCode();
print $myErrorMsg[$errorCode]; // array of my custom messages.
}
But getCode() method always returns zero for all possible errors.
My question for Recurly Team (or who there in this theme):
How i get error code for errors? Or please explain me how i can resolve this topic. Thanks!
If you look at the PHP Client on Github and you search for "throw new" which is what is done when an exception is thrown you'll see that they don't set the exception error code the second parameter of the exception constructor method.
Recurly PHP Client on Github: https://github.com/recurly/recurly-client-php/search?utf8=%E2%9C%93&q=throw+new
PHP Exception documentation: http://php.net/manual/en/language.exceptions.extending.php
Therefore, you'll either need to catch more exceptions based on their name
i.e.
catch (Recurly_NotFoundError $e) {
print 'Record could not be found';
}
OR
look at the exception message and compare it
catch (Exception $e) {
$errorMessage = $e->getMessage();
if($errorMessage=='Coupon is not redeemable.')
{
$myerrorCode=1;
}
//Add more else if, or case switch statement to handle the various errors you want to handle
print $myErrorMsg[$myerrorCode]; // array of my custom messages.
}
i am trying to connect to a webservice. My webserviceHelper is:
class webserviceHelper {
public function __construct($params) {
$this->service_url = $params['service_url'];
try {
$this->soap = new SoapClient($this->service_url,
array('exceptions' => true));
}
catch (SoapFault $exc) {
echo 'SoapFault<br />';
die;
}
catch (Exception $exc) {
echo 'Exception<br />';
die;
}
}
...
}
When the service is down, i make a request to the page where the webserviceHelper object created. Before the response i make second request to the same page. At first one, i got "soapFault" as output but at the second, i got a fatal error.
Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'WebService?wsdl' : failed to load external entity "WebService?wsdl" in webserviceHelper.php on line 40
How can i prevent this error?
use error_get_last() after $this->soap = new SoapClient(..... to get potential errors
I handled it by using a hook in codeigniter. Thanks to the blogger. How To Catch PHP Fatal Error In CodeIgniter
I am trying to catch when an email fails so that I can save the required data in my database and I can attempt to send at a later date.
I thought the following should work as it does when using save()
if ( $email->send() ) {
//..success - works..
} else {
//..fail - never gets here, stack trace
}
obviously you are not in debug mode there.
if you were, you would see that this actually throws an exception.
and you are catching sth there, just not the exception thrown :)
try this:
try {
$success = $email->send();
...
} catch (SocketException $e) { // Exception would be too generic, so use SocketException here
$errorMessage = $e->getMessage();
...
}
this way you can catch the exception and do sth here.
Which of the following is better to catch an error when calling a web service using SoapClent?
try {
$response = $client->SomeSoapRequest();
}
catch(SoapFault $e){
}
Or:
try {
$response = $client->SomeSoapRequest();
}
catch(SoapFault $e){
}
catch(Exception $e){
}
Also, I want to catch a socket timeout; will this be a SoapFault or an Exception?
Thanks!
Just catch Exception; this will also catch SoapFault. If you need to know the difference, you can check the type of the object received. Exception will also catch other non-soapfault exceptions, which you should be doing anyway. So, the answer is: the second one.
you can find some answers at this similar question.