I am using Amazon SES for sending emails, my final sending code being
try {
$result = $sesClient->sendEmail($email);
$messageId = $result->get('MessageId');
$result['success'] = $messageId;
} catch (Aws\Ses\Exception\SesException $e) {
$result['error'] = $e;
}
At the end of my query loop I want to gather all the errors and having them sent by email, but the problem is that only one error has about 7000 characters and that is because either if I catch Exception or Aws\Ses\Exception\SesException, I also get information from GuzzleHttp\Exception\RequestException: 'GuzzleHttp\Exception\ClientException' and many other infos which I do not realy need. Is there any way I can restrict the message with the main error message, which, in my case, was using an email with no #domain.com attached.
} catch (Aws\Ses\Exception\SesException $e) {
$result['error'] = $e->getMessage();
}
Related
I am using RabbitMQ with PHP. While consuming messages from RabbitMQ, we have magic in this piece of code:
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
}
}
This is working as infinite loop to receive messages as expected but what happening is if we lost connection to the RabbitMQ/RabbitMQ not up its going to catch block and returns nothing and printing bulk log messages. Is there any better way we can check for connection for RabbitMQ and stops the script? How can we achieve this? Any suggestions?
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
break;
}
}
I am using Stripe for a german website, I could translate the JS (stripeResponseHandler) error messages for the form following this post but not the exception of validation ($e->getMessage()).
I get the exception error in english, how can I translate it?
My PHP :
try {
require_once('Stripe/init.php');
\Stripe\Stripe::setApiKey("myKey"); //Secret Key
$token = $_POST['stripeToken'];
$coupon = \Stripe\Coupon::retrieve($_POST['couponId']);
$charge = \Stripe\Charge::create(array(...)
} catch (Exception $e) {
echo $e->getMessage();
}
The error messages returned by the API are in English -- at this time, Stripe does not have support for localized API error messages.
You can do what's described in the SO answer you linked to provide your own translations via error handling. E.g. you could do something like:
try {
$charge = \Stripe\Charge::create(...);
} catch(\Stripe\Error\Card $e) {
$body = $e->getJsonBody();
$err = $body['error'];
switch ($err['code']) {
case 'card_declined':
// use your own "card declined" error message
break;
case 'invalid_number':
// use your own "invalid number" error message
break;
// etc.
}
} catch (\Stripe\Error\Base $e) {
// another Stripe error happened, use a generic "error with our payment processor" message
} catch (Exception $e) {
// something else happened, unrelated to Stripe
}
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 have a function inside of a Component to send contact mail using Gmail. For whatever reason my work network is blocking the secure connection to Gmail if i use my phone internet for example it works fine so it's clear that the email configuration is fine. What i'm really interested is in catching that FatalErrorException so i can return false to the controller and show an error message in the web site but i'm failing to do so.
This is my code of the component:
try{
$correo = new Email();
$correo
->transport('mail')
->template('contacto_negocio')
->emailFormat('html')
->to($correoPara)
->from($correoDe, $nombreDe)
->replyTo($correoDe, $nombreDe)
->subject(__('Mensaje de contacto desde ').$sitio_nombre_secundario)
->viewVars([
'sitio_nombre_secundario'=>$sitio_nombre_secundario,
'sitio_nombre' => $sitio_nombre,
'nombreDe'=>$nombreDe,
'correoDe'=>$correoDe,
'mensaje'=>$mensaje
])
->send();
} catch (FatalErrorException $ex){
return false;
} catch(SocketException $ex){
return false;
}catch (\Exception $ex) {
return false;
}
And the code in the controller:
if($this->Correo->contactoNegocio($correo, $nombreDe, $correoDe, $mensaje)){
$respuesta = ['cod'=>1, 'mensaje'=>'Message sent'];
}else{
$respuesta = ['cod'=>0, 'mensaje'=>'<span style="color:red; font-size: 12px;">Message not sent.<span>'];
}
What i am missing??
Your code does not reach the point that it can catch FatalErrorException because the execution time error is more or less the result of a shutdown function that cake handles on its own.
You can increase the limit or try to modify their shutdown method, but you won't be able to "catch" this as an exception as it stops execution before the catch block.
I am trying to create a subscribe method for my laravel app that uses the mailchimp api to subscribe a user to a given list. The method works fine when the email address is not already on the lsit. when it is already subscribed the mailchimp api throws the following error
Mailchimp_List_AlreadySubscribed blah#blah.co is already subscribed to
list Tc App Test List. Click here to update your profile.
with the following code being shown
public function castError($result) {
if($result['status'] !== 'error' || !$result['name']) throw new Mailchimp_Error('We received an unexpected error: ' . json_encode($result));
$class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mailchimp_Error';
return new $class($result['error'], $result['code']);
}
I have attempted a try catch block to catch the error but it is still being returned to the browser, here is what I tried and were it says MailChimp_Error I tried with Exception as well.
public function subscribe($id, $email, $merge_vars)
{
try {
$this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (MailChimp_Error $e) {
$response = 'an error has occured';
}
return $response;
}
Ultimately I want to be able to run the method and then either return either a success message or a message describing the issue to the user. the 3 possible mailchimp method errors are Email_notexists, list_alreadysubscribed and list does not exist although tihs last one should not occur as I am providing the list in the source code.
edit 1; after being in touch with mailchimp api support they suggested this code but the error still gets returned to the browser in its entirety
try {
$results = $this->mailchimp->lists->subscribe($id, $email, $merge_vars);
} catch (Mailchimp_Error $e) {
if ($e->getMessage()) {
$error = 'Code:'.$e->getCode().': '.$e->getMessage();
}
}
echo $error;
You can do
try
{
$response = $this->mailchimp->lists->addListMember($list_id, [
"email_address" => $email,
"status" => "subscribed",
]);
}
catch (\EXCEPTION $e) {
return $e->getMessage();
}
The \EXCEPTION handles a sort of error for stripe
Subscribe is in a namespace Acme\Emails\Subscribe so catch(Mailchimp_Error $e) looks for Mailchimp_Error in this namespace.
Changing it to catch(\Mailchimp_Error $e) makes it look in the root namespace and then it works as intended