2

I am trying to send SMS to phone numbers entered by users into my applications, I'm making an AJAX post request using AXIOS to the PHP functions that send the SMS using NEXMO's API. When message delivers I have no issue, but whenever it does deliver, i get an Internal Server Error

enter image description here

This is the NEXMO Code I am using.

$message = Nexmo::message()->send([
    'to' => '234' . $phone->number,
    'from' => 'NEXMO',
    'text' => $text
]);

Which is working fine when phone number is rightly formatted and also if there is good network to deliver the SMS, but once there is any issue in sending the SMS, it generates an internal server error.

I have tried using a TRY and CATCH like below

try
{
  $message = Nexmo::message()->send([
    'to' => '234' . $phone->number,
    'from' => 'NEXMO',
    'text' => $text
  ]);
}catch(Exception $e){
  return $e;
}

It won't work.

I also try returning the value of $message['status'] and $message['err-code']. It only works when message successfully sends other times it returns an Internal Server error still.

Please how do I catch a situation where my message didn't get sent and report back to user through AJAX

John David
  • 752
  • 2
  • 9
  • 21
  • A 500 error should leave useful error details in your Laravel logs somewhere. Take a look in `storage/logs` or your webserver's logs. – ceejayoz Dec 04 '17 at 23:42
  • I'm aware of what the problem is, the API couldn't send the message to the phone number provided, and that could happen for many reasons, I only need away to catch it and give the user a useful information like "something went wrong", rather than just a crash. Did you get tthat? @ceejayoz – John David Dec 05 '17 at 00:17
  • 2
    Per your logs, is an exception being thrown? Is the code you're trying to `catch(Exception)` namespaced? If so, you'll probably need to do `catch(\Exception)` instead. – ceejayoz Dec 05 '17 at 00:21
  • @ceejayoz thanks for the attention to my question, I probably was just stressed up, and couldn't figure out I was to catch the exception in javascript and then output whatever to the user, woke up this morning and my head went straight to it. Will update with my answer. – John David Dec 05 '17 at 07:31
  • 2
    @ceejayoz reply was correct, just add the slash. OP it's always better to catch the error server side and respond to the client with your own message – MikeyBeLike Dec 15 '17 at 19:33
  • Yeah, you are right. It's better to catch the exception on the server-side. – John David Aug 17 '19 at 12:09

0 Answers0