Pranav Mandlik
Pranav Mandlik

Reputation: 644

want to set exception for twilio

i am sending otp using twilio,laravel, message is working now, but i want to set exception for if message is not delivered etc i have tried like

 public function send_otp()
{

    try {
            $account_sid = env('TWILIO_ACCOUNT_SID'); 
            $auth_token = env('TWILIO_AUTH_TOKEN'); 
            $number=Auth::user()->user_phone;
            $client = new Client($account_sid, $auth_token); 
            $messages = $client->messages->create($number, array( 
                 'From' => '+12533368077',
                 'Body' => Auth::user()->user_otp,
            ));
                dd($messages);
                //return $messages;
        //throw new Exception();

  }  catch (Exception $e) {

         return response()->json(['error' => true,'message'=>'Something went wrong'],200);
 }

}

can you please help me with this

Upvotes: 1

Views: 255

Answers (1)

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

After setting env data did you clear cache?

php artisan config:cache

If you want to handle error - laravel has special logic for that. You need to just to catch that error and then make action, it is simple: https://laravel.com/docs/5.6/errors

public function render($request, Exception $exception)
{
    if ($exception instanceof CustomException) {
        return response()->view('errors.custom', [], 500);
    }

    return parent::render($request, $exception);
}

Upvotes: 1

Related Questions