Skip to content

Should you define your own exception classes? #2

@shun-liang

Description

@shun-liang

It appears to me that you do not raise your own exceptions in the APIs, but instead just the built-in Exception, or just propagate the ConnectionError exception from requests.

Take geckoboard-python/geckoboard/datasets_client.py for example:

    def find_or_create(self, dataset_id, fields, unique_by=None):
        """
        Creates a new, or returns an existing dataset
        Searches for a dataset with a matching ID and schema. If a
        match is found that dataset is returned. Otherwise, a new
        dataset is created and returned.
        The dataset instance that is returned allows you
        to update or delete that dataset.
        ...
        Raises
        ------
        Exception
            If your API key is invalid or an existing dataset is found
            with a schema that doesn't match the one provided
        ConnectionError
            If you could not connect to the Geckoboard API
        """

This makes exception handling difficult for your users, as we are not able to handle exceptions specifically related to Geckoboard connections while other things can go wrong in the same try...except block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions