Laravel 中的 Nexmo 处理错误

Nexmo Handling Errors in Laravel

我如何处理 nexmo 错误,我使用 try{}catch(){} 但它不起作用,我得到了这个错误 Nexmo \ Client \ Exception \ Request (29) 非白名单目的地 - 被拒绝 我知道这个错误,但我需要处理它。

这是一个代码:

<?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();
    }
 }  
}

我过去通过在 try catch 中包装对 notify() 的调用来解决这个问题:

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())
}

如果您 post 您正在呼叫的线路 notify() 我可以更新为您使用完全需要的声明。

ConfirmedCourse class 中删除 try catch 并将其放在调用它的方法中的调用周围。

以下是我处理松弛通知失败的方式:

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