Skip to content

Give users a way to choose how they want to handle errors.  #613

@djachkov

Description

@djachkov

Description

First of all, I've struggled with the issue while finding a user by email address:

File "*****/lib/python3.8/site-packages/slack/rtm/client.py", line 529, in _wait_exponentially
    wait_time = exception.response["headers"]["Retry-After"]
TypeError: 'NoneType' object is not subscriptable

Trying to understand which behavior raised that issue, I found, that it is the consequences of the same issue, as in #579
This isn't normal behavior, I think. Why do we try to WAIT if we already have an error? (a user that we trying to find does not exist at all)
The root cause of this is the way, the library handles the errors. Getting the error, we're trying to validate SlackResponse object and raise an error each time it status_code isn't 200.
According to #579 I have to use try/except block in my code to handle the errors.
But for my use case it isn't a good approach:
I'm trying to make a mapping between the list of email addresses from JSON file (which can contain email addresses, that doesn't exist in Slack) and the Slack users (to send a message mentioning them later)
I use generator expressions with the async calls to make it cleaner and easier (at least for me):

users = (await command.web_client.users_lookupByEmail(email=e) for e in emails)

I use this generator to feed another generator and make a pipeline.
Using that approach makes using the try/except blocks a little harder.
It would be great if instead of stopping my code in such a straightforward way, the library can just return empty/error/different value for me to handle it later.
We can implement this as easy as just make another additional flag in the BaseClient initialization, like handle_errors=False and check this flag in the self.validate() function:

        if self.handle_errors:
            raise e.SlackApiError(message=msg, response=self)
        else:
            return e.SlackApiError(message=msg, response=self)

So my suggestion has the following pros and cons:

  • Giving users choosing how to handle errors
  • Easy to implement
  • Have a back-compatibility
  • Returning errors instead of SlackResponse might be not so explicit as raising an error (but this doesn't affect so much because we using an optional flag)

Hope that it will be useful because I really do not want to make bulky loops with try/except blocks from my little cute generators

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Reproducible in:

slackclient version: 2.5.0

python version: 3.8.0

OS version(s): Any

Steps to reproduce:

  1. Use users_lookupByEmail(email="this_address_does_not_exist@example.com")
  2. Get an error
  3. Be sad

Expected result:

While performing users_lookupByEmail() for the list of emails get results for ALL emails in the list

Actual result:

I can't get anything because the library just raises an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Version: 2xdiscussionM-T: An issue where more input is needed to reach a decisionenhancementM-T: A feature request for new functionalityweb-client

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions