PHP: How to take exception from the model in the controller

Asked

Viewed 156 times

-1

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?

  • Not if you have anything standard you should create your custom Exception class and then capture in catch.

  • But the blocks try/catch will only evaluate the exception thrown by the method within them? Or another exception may be cast, beyond that which I myself define?

  • 1

    The block try/catch given should be capturing the exception. The class Ldap is part of some namespace?

  • Yes. It’s part of namespace Models

2 answers

2


First, if the Ldap class is present within some namespace, for you to shoot and/or capture the Exception, you need to put it in the global namespace, that is, shoot and capture using \Exception.

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

class LdapException extends \Exception{}

So in your method Ldap::ldapConnection() 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()));
}

0

I used the idea of @Lucasmendes and got the following result:

I created the class of exceptions extending the class \Exception, but I had to make a modification: add the builders.

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();
}
  • 1

    Why did you have to add the builder when in the same you made the direct call to the parent-class builder? If you don’t have too much logic, you don’t need to overwrite the method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.