Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[LDAP] Add error codes to exceptions #28677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shanept opened this issue Oct 2, 2018 · 4 comments
Closed

[LDAP] Add error codes to exceptions #28677

shanept opened this issue Oct 2, 2018 · 4 comments

Comments

@shanept
Copy link

shanept commented Oct 2, 2018

Description
Upon LDAP errors, the PHP ldap extension may also return an error code. Using this code may provide a friendlier way to determine the root cause of an issue than to interpret the error message - consider how the following example may be achieved currently, with the LDAP component.

Example

$ldap = Ldap::create('ext_ldap', [...]);

try {
    $ldap->bind('invalid_user', 'invalid_password');
} catch (ConnectionException $ex) {
    switch ($ex->getCode()) {
        case INVALID_CREDENTIALS:
            $message = 'Invalid Username or Password.';
            break;
        case CONNECTION_TIMEOUT:
            $message = 'Unable to connect to server.';
            break;
        ...
    }
}
@scaytrase
Copy link
Contributor

I'd better suggest throw proper exception with given messages

@fabpot fabpot closed this as completed Jun 22, 2019
fabpot added a commit that referenced this issue Jun 22, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Ldap] Add exception for mapping ldap errors

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #28677   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Maybe we could add more exception code since the list has a lot of errors, maybe we could add a class that maps the error to the right exeptions. see https://www.php.net/manual/en/function.ldap-errno.php

Commits
-------

1b29cb1 [Ldap] Add exception for mapping ldap errors
@dontub
Copy link

dontub commented Jul 26, 2019

I think this is not only relevant for the bind operation, but also for other operations e.g. query. For example I want to catch LDAP_NO_SUCH_OBJECT (error code 32). Currently I'd have to parse the message to do this. So I suggest to reopen this issue.

@fabpot
Copy link
Member

fabpot commented Aug 6, 2019

@dontub Would you will to submit a pull request for that?

@dontub
Copy link

dontub commented Aug 6, 2019

I just had a deeper look at it. I'm currently thinking about an exception factory like this

final class ExceptionFactory
{
    public static function createLdapException($messageFormat, $connection)
    {
        $errorCode = ldap_errno($connection);
        $errorMsg = ldap_err2str($errorCode);
        $message = strtr($messageFormat, [
            '%errorCode%' => $errorCode,
            '%errorMsg%'  => $errorMsg,
        ]);

        return new LdapException($message, $errorCode);
    }
}

instead of adding a call to ldap_errno() everywhere.

I also found four places where an LdapException is thrown without a call of an LDAP operation:

Is the last one necessary as the OptionsResolver would already prevent this?
Should the LdapException be limited to errors of real LDAP operations? If yes, what would be an appropriate replacement for the exceptions above? The class comment of LdapException (LdapException is thrown if php ldap module is not loaded.) needs to be updated in any case. (The same holds true for DriverNotFoundException which has exactly the same comment.)

WDYT?

In case of the exception factory: Should PHP type hints be used there?

dontub pushed a commit to dontub/symfony that referenced this issue Aug 26, 2019
This PR ensures that an LdapException is only used when an LDAP
operations fails. In cases an LdapException was used for non LDAP
operations it is replaced by an appropriate exception.
dontub pushed a commit to dontub/symfony that referenced this issue Oct 9, 2019
This PR ensures that an LdapException is only used when an LDAP
operations fails. In cases an LdapException was used for non LDAP
operations it is replaced by an appropriate exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants