【问题标题】:Nexmo Handling Errors in LaravelLaravel 中的 Nexmo 处理错误
【发布时间】:2020-03-25 04:38:16
【问题描述】:

如何处理 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 (\Exception $e) { $e-&gt;getMessage()}
  • 我今天刚刚优雅地修复了通知错误处理。你从哪里调用通知?我可以看看那行代码吗
  • $yourModel-&gt;notify(....的通话
  • 我编辑了我的帖子,仍然有错误,是的,是通知。

标签: laravel vonage


【解决方案1】:

我过去通过在 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())
}

如果您在调用notify() 的位置发布行,我可以更新为您使用确切需要的语句。

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

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

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

【讨论】:

  • 很好,非常感谢你:),我还有一个问题,如何从 $e 获取错误详细信息?
  • 那就是$e-&gt;getMessage()
  • 哦,是的,再次感谢你 :) :)
  • 没问题,很高兴能帮上忙。如果解决了,请继续并将其标记为正确答案 :) 祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 2018-07-02
  • 2013-10-07
  • 2015-03-09
  • 2016-06-08
  • 2016-01-12
  • 2013-12-08
相关资源
最近更新 更多