Twilio sms not handling exception - php

My application sends an sms message when a user signs up. I'm trying to handle the exception if the number is invalid, and Twilio fails to send it. But when the message fails, it never seems to reach the catch in my try/catch clause.
try {
$message = $twilio->messages
->create(
"$user->phone", // to
array("from" => "$siteNumber", "body" => "$message_text")
);
} catch (TwilioException $e) {
return $e->getMessage();
}
I've also tried the following catch conditions.
} catch (\Services_Twilio_RestException $e) {
return $e->getMessage();
}
catch (RestException $e) {
return redirect()->back()->withFlashSuccess("Failed To Send Text");
}

Please try to change the catch exception
catch (\Exception $e) {
return $e->getMessage();
}
It's working for me

Related

Exception not able to get caught in catch block - Yii2

Exception
Exception 'Error' with message 'Class 'app\commands\CallLogs' not
found'
is not able to get caught in catch block.
Code:
I tried with calling undefined class just to see how and what exception catch block catches.
public function actionTest(){
try {
$logs = new CallLogs();
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
But, When I intentionally throw any exception, it works.
public function actionTest(){
try {
throw new \yii\base\Exception('hello');
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
I have tried with base\Exception class and \ErrorException class. But, no help.
Any help/hint is appreciable
catch (\Throwable $e) will do the job
\Throwable was introduced back in PHP 7.0 and is (quoting from docs) used for
[...] any object that can be thrown via a throw statement, including
Error and Exception.

How to throw custom error in PHP mysql

$row=mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error())
In this code the mysql_error will be displayed if mysql_fetch_array fails. Is there any way to throw that mysql_error to reload a page instead.
P.S.: I know mysql has been depreciated. Its an old project so please don't suggest in your answers to use mysqli and PDO. I am well aware of it. Please tell me the way to throw reload page as error.
You could use try catch for exception handling in PHP
http://www.php.net/manual/en/language.exceptions.php
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something wrong', 0, $e);
}
OR
try
{
// do something that can go wrong
}
catch (Exception $e)
{
if ($e instanceof CustomException)
{
// do something
}
else if ($e instanceof OtherException)
{
// do something
}
else
{
// do something
}
}
Or
try
{
}
catch (CustomException $e)
{
}
catch (OtherException $e)
{
}
catch (Exception $e)
{
}

Try-Catch all Exceptions with Zend_Http_Client?

I have the following function:
public function getClientTable($feedUrl)
{
$client = new Zend_Http_Client($feedUrl);
try
{
return $client->request()->getBody();
}
catch (Zend_Http_Client_Adapter_Exception $e)
{
return false;
}
}
It seems to work great for catching that specific Zend_Http_Client_Adapter_Exception; but what if I want it to catch additional exceptions? Hell, what if I wanted it to catch ALL exceptions... how would I do this?
Also, should I be using "return" or "throw" in the try? Why does it matter?
You can have multiple catch statements, eg
try {
// whatever
} catch (Zend_Http_Client_Adapter_Exception $e) {
// ah ha
} catch (Zend_Some_other_Exception $e) {
// ah ha
} catch (Exception $e) {
// And the final fallback catch that grabs all exceptions
}

Rethrow php exception into higher level catch block

I am trying to pass an exception from a specific catch block to a more general catch block. However it does not appear to be working. I get a 500 server error when I try the following. Is this even possible?
I realize that there are easy workarounds, but isn't it normal to say, "Hey I don't feel like dealing with this error, let's have the more general exception handler take care of it!"
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
catch (Exception $e) {
echo $e->getMessage();
}
Technically this is what you're looking for:
try {
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
}
catch (Exception $e) {
echo $e->getMessage();
}
however I agree that exceptions shouldn't be used for flow control. A better way would be like this:
function show_error($message) {
echo "Error: $message\n";
}
try {
//some soap stuff
}
catch (SoapFault $sf) {
show_error('Soap Fault');
}
catch (Exception $e) {
show_error($e->getMessage());
}

php try ... else

Is there something similar in PHP to the try ... else in Python?
I need to know if the try block executed correctly as when the block executed correctly, a message will be printed.
PHP does not have try/catch/else. You could however set a variable in the catch block that can be used to determine if it was run:
$caught = false;
try {
// something
} catch (Exception $e) {
$caught = true;
}
if (!$caught) {
}
I think the "else" clause is a bit limiting, unless you don't care about any exceptions thrown there (or you want to bubble those exceptions)... From my understanding of Python, it's basically the equivalent of this:
try {
//...Do Some Stuff Here
try {
// Else block code here
} catch (Exception $e) {
$e->elseBlock = true;
throw $e;
}
} catch (Exception $e) {
if (isset($e->elseBlock) && $e->elseBlock) {
throw $e;
}
// catch block code here
}
So it's a bit more verbose (since you need to re-throw the exceptions), but it also bubbles up the stack the same as the else clause...
Edit Or, a bit cleaner version (5.3 only)
class ElseException extends Exception();
try {
//...Do Some Stuff Here
try {
// Else block code here
} catch (Exception $e) {
throw new ElseException('Else Clasuse Exception', 0, $e);
}
} catch (ElseException $e) {
throw $e->getPrevious();
} catch (Exception $e) {
// catch block code here
}
Edit 2
Re-reading your question, I think you may be overcomplicating things with an "else" block... If you're just printing (which isn't likely to throw an exception), you don't really need an else block:
try {
// Do Some stuff
print "Success";
} catch (Exception $e) {
//Handle error here
print "Error";
}
That code will only ever print either Success or Error... Never both (since if the print function throws the exception, it won't be actually printed... But I don't think the print CAN throw exceptions...).
Not familiar with python but it sounds like you're after Try Catch blocks used with exceptions...
http://php.net/manual/en/language.exceptions.php
There is try-catch in php.
Example:
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
else return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
try {
$clean = false;
...
$clean = true;
} catch (...) { ... }
if (!$clean) {
//...
}
That's the best you can do.
You can use try { } catch () { } and throw. See http://php.net/manual/en/language.exceptions.php
try {
$a = 13/0; // should throw exception
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
or manually:
try {
throw new Exception("I don't want to be tried!");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

Categories