Catch db error with Doctrine\DBAL\Exception
<p>I want to catch any error (ie: foreign key error) from an <code>insert</code> statement or any other. How can I achive that with <code>use Doctrine\DBAL\Exception</code>?</p>

<p>I have this when I do the <code>insert</code>: </p>

<pre><code>            $db-&gt;beginTransaction();
            try {
              $db-&gt;insert('bbc5.produccionpry',$datos);
              $datos['propryid'] = $db-&gt;lastInsertId();
              $db-&gt;commit();
              // $db = null;
              $resp[] = $datos;
            } catch (Exception $e) {
              $error = array_merge($error, array('error' =&gt; $e-&gt;errorInfo()));
              $db-&gt;rollback();
              throw $e;
            }
</code></pre>

<p>But, that doesn't prevent the concrete5 to <code>return</code> a website telling about the error, so, I don't want that website to be shown, I want to catch the error in an <code>array()</code> in order to return it via <code>echo json_encode($error)</code></p>

<p>I'm not using the controller for a page, I'm using it for managing RESTful calls from my JavaScript App with this code: </p>

<pre><code>return fetch(`/scamp/index.php/batchprodpry/${maq}`, { 
  method: 'POST', 
  credentials: 'same-origin', 
  headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/json' 
  }, 
  body: JSON.stringify(this.state.a) 
})
</code></pre>

<p>I'm using ReactJS</p>

<p>Thank you</p>