title: Eloquent-oauth - catching PDO Exception
body: <p>Although this question is about a specific package it should be relevant to other packages as it is really about where in the Laravel framework to correctly trap for the situation below.</p>
  
  <p>I am using <a href="https://github.com/adamwathan/eloquent-oauth" rel="nofollow">eloquent-oauth</a> to enable login via linkedin and facebook to my app.</p>
  
  <p>Authorizing and logging in both work fine individually. </p>
  
  <p>However if I have previously authorized and logged in via one social platform and then try to authorize with another I get a PDO exception that the insert fails on the unique email constraint on my user table.</p>
  
  <p>Where should I trap for that insertion? </p>
  
  <p>I can see that I can add in code to the vendor folder directly but don't think that is good practise as that would surely be overwritten if I upgraded the package at some point in the future?</p>
  
  <p>What I am currently doing is to add a user check into the vendor/adamwathan/eloquent-oauth/src/OAuthManager.php file in the login function</p>
  
  <pre><code>public function login($providerAlias, Closure $callback = null)
  {
      if (! $this-&gt;stateManager-&gt;verifyState()) 
      {
          throw new InvalidAuthorizationCodeException;
      }
      $details = $this-&gt;getProvider($providerAlias)-&gt;getUserDetails();
  
      //start of existing user check
      $thisUser = User::where('email', $details-&gt;email)-&gt;first();
      if ($thisUser !== null)
          {
              $autoOpenModalLogin = true; //instruct the page to open the login modal
              return redirect()-&gt;guest('home')-&gt;with('$autoOpenModalLogin', $autoOpenModalLogin);
          }
      //end of existing user check
      return $this-&gt;authenticator-&gt;login($providerAlias, $details, $callback);
  }
  </code></pre>
  
  <p>I thought it might be an better to add it into the route somewhere - but I can't seem to trap that error. Is there something else I should add below to catch a PDO error?</p>
  
  <pre><code>Route::get('{provider}/login', function ($provider) {
  try {
      OAuth::login($provider, function ($user, $userDetails) {
      $user-&gt;email = $userDetails-&gt;email;
      $user-&gt;name = $userDetails-&gt;firstName . ' ' . $userDetails-&gt;lastName;
      $user-&gt;first_name = $userDetails-&gt;firstName;
      $user-&gt;last_name = $userDetails-&gt;lastName;
      $user-&gt;save();
      });
      return view('home');
  } catch (ApplicationRejectedException $e) {
      // User rejected application
  } catch (InvalidAuthorizationCodeException $e) {
      // Authorization was attempted with invalid
      // code,likely forgery attempt
  }
  });
  </code></pre>
  
  <p>I guess if I keep it the inserted user check in the vendor files I will just need to update those if I ever update the package?</p>
  
  <p>But is there a better way to do it than what I have done? </p>