PHP: How to catch exception thrown by the model in the controller

0

I created a class to connect to LDAP, in one of the methods I did the following:

// Classe Ldap()
public function ldapConnection() {

    $this->_ldapCon = ldap_connect($this->_ldapServer);

    if ($this->_ldapCon) {

        ldap_set_option($this->_ldapCon, LDAP_OPT_PROTOCOL_VERSION, 3);

        ldap_set_option($this->_ldapCon, LDAP_OPT_REFERRALS, 0);

        return $this->_ldapCon;

    } else {

        throw new Exception('Não foi possível conectar ao servidor LDAP');

    }

}

If I do

$conexao = new Ldap();

try {

    $conexao->ldapConnection();

} catch (Exception $ex) {

    // Abaixo, não pega a exceção que eu lancei no método. A exceção lançada no método aparece como "Fatal error: Uncaught exception 'Exception' with message: Não foi possível conectar ao servidor LDAP"
    echo json_encode(array('message' => $ex->getMessage()));

}

How do I get the exact exception thrown by the LDAP class method and not just a generic exception?

    
asked by anonymous 22.03.2017 / 21:38

2 answers

2

First, if the Ldap class is present within some namespace, you have to put it in the global namespace, that is, to fire and capture it using \Exception .

To get a specific Exception for the LDAP class, you first have to create your own Exception class, for example:

class LdapException extends \Exception{}

Then in your Ldap::ldapConnection() method you put:

// Classe Ldap()
public function ldapConnection() {

    $this->_ldapCon = ldap_connect($this->_ldapServer);

    if ($this->_ldapCon) {
        ldap_set_option($this->_ldapCon, LDAP_OPT_PROTOCOL_VERSION, 3);

        ldap_set_option($this->_ldapCon, LDAP_OPT_REFERRALS, 0);

        return $this->_ldapCon;
    } else {
        throw new LdapException('Não foi possível conectar ao servidor LDAP');
    }
}

And to capture:

$conexao = new Ldap();

try {
    $conexao->ldapConnection();
} catch (LdapException $ex) {
    echo json_encode(array('message' => $ex->getMessage()));
}
    
23.03.2017 / 05:42
0

I used the idea of @LucasMendes and I came up with the following result:

I created the exception class by extending the class \Exception , however I had to make a modification: add the constructors.

namespace Models;

class AppExceptions extends \Exception
{
    public function __construct($message, $code = 0, \Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}

Then in the Ldap () class:

protected function conexaoLdap()
{
    $this->_conexao = ldap_connect($this->servidor_ldap);
    if ($this->_conexao) {
        ldap_set_option($this->_conexao, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($this->_conexao, LDAP_OPT_REFERRALS, 0);
        return $this->_conexao;
    } else {
        throw new AppExceptions('Não foi possível se conectar ao servidor LDAP');
    }
}

Taking the exception:

try {
    // Code goes here
} catch (AppExceptions $e) {
    echo $e->getMessage();
}
    
23.03.2017 / 18:01