Skip to content
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

Improve error messages #12

Open
alejandrodob opened this issue Mar 16, 2015 · 3 comments
Open

Improve error messages #12

alejandrodob opened this issue Mar 16, 2015 · 3 comments

Comments

@alejandrodob
Copy link
Contributor

For instance, I got a ProjectNotFound exception when trying to create a project (which already makes little sense in itself; as you are trying to create the project it could hardly be found). Investigating, the problem was that I got bad credentials (wrong api key).

@cosmocracy
Copy link
Contributor

While attempting to use PBS, I misconfigured the server/URL and yet PBS returns success:

Project: test_project created!

Adding extra debugging to check_api_error in helpers.py, it's clear that the code could use some improvement for robustness. For example, note that the code does not properly handle cases where there is no status value in the JSON response (e.g., my 404 error due to a bad URI).

def check_api_error(api_response):
    """Check if returned API response contains an error."""
    if type(api_response) == dict and (api_response.get('status') == 'failed'):
        if 'ProgrammingError' in api_response.get('exception_cls'):
            raise DatabaseError(message='PyBossa database error.',
                                error=api_response)
        if ('DBIntegrityError' in api_response.get('exception_cls') and
            'project' in api_response.get('target')):
            msg = 'PyBossa project already exists.'
            raise ProjectAlreadyExists(message=msg, error=api_response)
        if 'project' in api_response.get('target'):
            raise ProjectNotFound(message='PyBossa Project not found',
                                  error=api_response)
        if 'task' in api_response.get('target'):
            raise TaskNotFound(message='PyBossa Task not found',
                               error=api_response)
        else:
            raise exceptions.HTTPError

Instead the code should do something closer to:

def check_api_error(api_response):
    """Check if returned API response contains an error."""
    if 'status' not in api_response:
        msg = "Unable to find 'status' in server response; Misconfigured URL?"
        print(msg)
        print("Server response: %s" % api_response)
        raise Exception(msg)
    if type(api_response) == dict and (api_response.get('status') == 'failed'):
        if 'ProgrammingError' in api_response.get('exception_cls'):
            raise DatabaseError(message='PyBossa database error.',
                                error=api_response)
        if ('DBIntegrityError' in api_response.get('exception_cls') and
            'project' in api_response.get('target')):
            msg = 'PyBossa project already exists.'
            raise ProjectAlreadyExists(message=msg, error=api_response)
        if 'project' in api_response.get('target'):
            raise ProjectNotFound(message='PyBossa Project not found',
                                  error=api_response)
        if 'task' in api_response.get('target'):
            raise TaskNotFound(message='PyBossa Task not found',
                               error=api_response)
        else:
            raise exceptions.HTTPError

I'll create a PR of this for the team's consideration.

cosmocracy added a commit to cosmocracy/pbs that referenced this issue Oct 4, 2017
@cosmocracy
Copy link
Contributor

Ugh, nevermind. There are inconsistencies throughout the API/client so it's not a simple matter of adding a few checks. All of the PBS tests need to be refactored, ideally adding more simulated information to the mocks. I'm a bit surprised the status/status_code aren't part of the mock testing. That would've allowed for quick[er] refactoring of check_api_error. As it stands, I've invested a couple hours into this refactoring and don't feel confident about it. Will spin up another issue to cover improving the tests.

@teleyinex
Copy link
Member

Hi! Thanks a lot for all the hard work. We know we should improve the API checks, so any help is welcome! Please, check out our CONTRIB guidelines for merging your code. Once you have the PR we'll check it and merge it if it looks ok (which it will) :D

teleyinex added a commit that referenced this issue Feb 21, 2018
Additional error checking on API calls; See #12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants