Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:26:32+00:00 2026-05-27T21:26:32+00:00

Silex seems to hijack the exceptions thrown anywhere in the app. My problem with

  • 0

Silex seems to hijack the exceptions thrown anywhere in the app. My problem with this is I have a user_db_interface class I use to pass/get user data from my database that is basically ignored once an exception is thrown.

For example if I have a duplicate user email entry that generates a PDOException, instead of being able to handle that exception within my class, I have to add an error handling closure outside of my class. I’d rather deal with the exception internally so that I can act on this exception and continue with my script.

Any such thing as an exception override or bypass?

Here is an example:

###### PROVIDER REG #######
// I'M LOADING DOCTRINE "NATURALLY" AND NOT REGISTERING IT WITH $APP IN HOPES OF 
// AVOIDING THE EXCEPTION HIJACKING.
################################################################### LOAD DOCTRINE NATURAL

require 'vendor/Doctrine/Common/ClassLoader.php';

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__ . '/../vendor');
$classLoader->register();

$connectionOptions = array(
    'driver'    => 'pdo_mysql',
    'dbname'    => 'MY_DB',
    'host'      => 'localhost',
    'password'  =>  'MY_PASS',
    'user'      => 'MY_USER',
);

$driver = new \Doctrine\DBAL\Driver\PDOMySql\Driver;
$connection = new \Doctrine\DBAL\Connection($connectionOptions, $driver);

###################################################################### LOAD MY EXTENSION
$app['autoloader']->registerNamespaces(array('TableTrackPro' => __DIR__,));

$app->register(new TableTrackPro\ClientDBInterfaceExtension(),array('app'=>$app));
$app['client_db_interface']->set_connection($connection);


##################################################
##################################################

#################################################  MY DB INTERFACE CLASS (stripped for clarity)

namespace TableTrackPro;

class ClientDBInterface{

    private $_connection;

    public function set_connection($connection){
        $this->_connection = $connection;
    }

    public function insert_client($clientInfo){
        // SET RETURN OBJECT
        $returnObject['status'] = 'ok';     // default return status
        $returnObject['message'] = false;


// SANITIZE CLIENT INFO ETC...

 // THIS IS WHERE I WOULD LIKE TO HANDLE MY OWN EXCEPTIONS
    try{
        $this->_connection->insert('clients', $clientInfo);
    }catch(PDOException $e){
       // HANDLE EXCEPTION HERE
    }
    return json_encode($returnObject);
}


#### END CLASS #####


### CONTROLLERS ####

$app->post('/postNewClient', function () use($app){
    $clientInfo = $_POST;
    $returnObject = $app['client_db_interface']->insert_client($clientInfo);
    return json_encode($returnObject);  // <-- I handle this object with javascript via an AJAX call
});

Instead of handling the PDOException in my class so that I can return data as I wish, I have to add an error handler to the controller area like:

$app->error(function (\Exception $e, $code) {
   // error handling code, I'd now have to handle any client-db specific errors outside of my client_db class...
});

I’d like to avoid this because if the exception handling were internal to the client_db class, I would know that the error was also specific to that action and I wouldn’t have to write an error parser.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:26:33+00:00Added an answer on May 27, 2026 at 9:26 pm

    Alright, so the only thing silex does is catch the exception during the $app->run() call. There is nothing in there preventing you from catching the exception yourself earlier.

    I suspect that your issue is caused by the following. Assuming your code is actually:

    try{
        $this->_connection->insert('clients', $clientInfo);
    }catch(PDOException $e){
       // HANDLE EXCEPTION HERE
    }
    

    Well, the issue is that you’re in a namespaced class. This means that all class names are relative to the current namespace. To reference the global namespace you need to use them or prefix them with \.

    What you need to do is:

    try{
        $this->_connection->insert('clients', $clientInfo);
    }catch(\PDOException $e){
       // HANDLE EXCEPTION HERE
    }
    

    Does this solve your problem?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should work but I get a 404 error. My app
My application file: <?php // /src/app.php require_once __DIR__ . '/../lib/vendor/Sensio/silex.phar'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response;
I have a Silex + Twig app running just fine on Heroku when not
I'd like to run a Silex Application like this in Command Line: $app =
I have this svn:external file in a project: Silex https://github.com/fabpot/Silex/trunk Silex/vendor/Symfony/Component/BrowserKit https://github.com/symfony/BrowserKit/trunk Silex/vendor/Symfony/Component/ClassLoader https://github.com/symfony/ClassLoader/trunk
Normally, for a Silex project, I would have top-level directories like: - app/ -
I have created a class to ease the use of SQL server within my
I use Silex Frame Work and I am trying to delete files in backend
I would like to try Silex but i've some questions. I know to use
I've been following the documentation at the resource below http://silex.sensiolabs.org/doc/providers/form.html But I get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.