Doubts about exceptions in their own mvc pattern

0

Recently, I wanted to create my own framework using the MVC pattern to test my skills. (Clarification, it is developed in php and mysql is used for BD) My framework has the following architecture:

In summary:

  • From an instantaneous index.php of the route controller class and send the message controllerRutas-> begin (); which starts the whole process.
  • The route controller what it does is capture the URI, process it and determine which controller is going to be used and which method of that class is going to be called.
  • If the controller needs information from the database, it is requested from the model, and the model connects to the base and makes queries through the connection class.
  • Finally, the controller sends the information that I request to the corresponding view. PS: if the controller does not need information from the BD, it will simply call at sight.
  • The problem I have is with the exceptions. What I did was (although I do not think it's okay) to use a try / catch in the index (file that starts the execution of my application) to capture any exception that is released later (that is, in controller.php, modelo.php , connection.php, vista.php, etc) Simplifying details, it would be something like this:

    index.php

    $ruta = new controladorDeRutas();
    try{
        $ruta->comenzar();
    }
    catch(Excepcion404 $e){
        Vista::crearVista("nombreDeLaVistaAMostrar", $e);
    }
    catch(ExcepcionConexionABaseDeDatos $e){
        Vista::crearVista("nombreDeLaVistaAMostrar", $e);
    }
    ...etc
    

    That's why if an error is thrown in one of the files, I capture it in the initiator of my process (index.php). The issue is that I do not know if this is all right. It had occurred to me, on the other hand, to throw exceptions and capture them in the other files, instead of concentrating everything on the index. Example:

    conexion.php

    ...código...
    if($conexion->errno){
        throw new ExcepcionConexionABaseDeDatos;
    }
    ...código...
    

    modelo.php

    ...código...
    public function buscarTodosLosUsuarios(){
        $conexion = new Conexion();
        ...código...
    }
    ...código...
    

    controller.php

    public function mostrarUsuarios(){
       $modelo = new Modelo();
       try{
           $modelo->buscarTodosLosUsuarios();
           Vista::crearVista("nombreDeLaVistaAMostrar");
       }catch($e){
           Vista::crearVista("nombreDeLaVistaAMostrar", $e);
       }
    }
    

    I do not know if it was understood. In the first case I capture all the exceptions in the index with a single try / catch. In the second case you should try / catch for each exception that occurs, to capture it in the controller and show the corresponding view. In short (summary):

    • Should it capture all the exceptions thrown in the index?
    • Is it appropriate to capture the exceptions in each particular case from the controller?
    • Which option would you choose? Can you think of any other idea?

    If you read this far, thank you for taking the trouble to do so. Thank you very much.

        
    asked by cortigeronimo 07.04.2017 в 21:59
    source

    1 answer

    0

    It is not convenient to capture the exceptions in the index because you would not even know how many would exist, you would have to do each page to test everything. Each controller should address the possible errors that occur in the logic it handles.

    In your design you should include a pattern that allows you to handle the exceptions of a group, example: everything related to models. Look for patterns like " observe " to give you an example.

        
    answered by 07.04.2017 в 23:37