Search code examples
laravelvonage

Nexmo Handling Errors in Laravel


How I can handle nexmo errors, I use try{}catch(){} but it not work, and I got this error Nexmo \ Client \ Exception \ Request (29) Non White-listed Destination - rejected i know this error but i need handle it.

this is a code:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
//use App\admin\Course;

class ConfirmedCourse extends Notification
{
    use Queueable;
    protected $course;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($course)
    {
     $this->course = $course;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['nexmo'];
    }

 public function toNexmo($notifiable)
    {
      try {
     $message = new NexmoMessage(); 
     $message->content($this->course)
         ->unicode();
     return $message;
    }catch (\Exception $e) {
    $e->getMessage();
    }
 }  
}

Solution

  • I fixed this in the past by wrapping the call to notify() in the try catch:

    try {
        $variableToCatch = $YourModel->notify(new ConfirmedCourse($data));
    } catch (\Exception $e) {
        // Do what you want here... 
        // Log::error('nexmo failed...');
        // echo 'Caught exception: ',  $e->getMessage(), "\n";
        // Log::error($e->getMessage());
        // dd($e->getMessage())
    }
    

    If you post the line where you are calling notify() I can update to your use exact needed statement.

    Remove the try catch from ConfirmedCourse class and put it around the call in the method calling it.

    Here is how I handled a slack notification failure:

    try {
        $slackNotification = $user->notify(new SlackNotification($slackData));
    } catch (\Exception $e) {
        Log::error('slack notification failed.');
    }