将异常错误消息返回到变量Laravel中

时间:2018-11-22 17:30:47

标签: laravel exception

我正在做一个功能来在laravel中使用Firebase登录,它可以正常工作,但是我试图将错误消息捕获到会话变量中,然后重定向到登录视图,但是显示了laravel错误视图。我如何避免这种情况?谢谢!

enter image description here

public function login(Request $data) {
        $email = $data->input('email');
        $password = $data->input('password');

        try {
            $user = $this->auth->verifyPassword($email, $password);
            Session::put('user_uid', $user->uid);
            Session::put('user_email', $user->email);
            return redirect('/');
        } catch (Kreait\Firebase\Exception\Auth\InvalidPassword $e) {
            $message = $e->getMessage();
            Session::put('message', $message);
            return redirect('login');
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要将Kreait\Firebase\Exception\InvalidArgumentException捕获为

public function login(Request $data) {
    $email = $data->input('email');
    $password = $data->input('password');

    try {
        $user = $this->auth->verifyPassword($email, $password);
        Session::put('user_uid', $user->uid);
        Session::put('user_email', $user->email);
        return redirect('/');
    } catch (\Kreait\Firebase\Exception\Auth\InvalidPassword $e) {
        $message = $e->getMessage();
        Session::put('message', $message);
        return redirect('login');
    } catch (\Kreait\Firebase\Exception\InvalidArgumentException $e) {
        $message = $e->getMessage();
        Session::put('message', $message);
        return redirect('login');
    }
}

如果您使用的是PHP 7.1 +

public function login(Request $data) {
    $email = $data->input('email');
    $password = $data->input('password');

    try {
        $user = $this->auth->verifyPassword($email, $password);
        Session::put('user_uid', $user->uid);
        Session::put('user_email', $user->email);
        return redirect('/');
    } catch (\Kreait\Firebase\Exception\Auth\InvalidPassword | \Kreait\Firebase\Exception\InvalidArgumentException $e) {
        $message = $e->getMessage();
        Session::put('message', $message);
        return redirect('login');
    }
}