Skip to content

Commit

Permalink
Add custom exceptions (#3)
Browse files Browse the repository at this point in the history
* Add custom exceptions

* Fix exceptions type annotation for status code in handler
  • Loading branch information
sanjacob authored Aug 11, 2024
1 parent dc48db6 commit c0c1f55
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
10 changes: 5 additions & 5 deletions blackboard/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Blackboard API.
Blackboard API
an interface to make Blackboard REST API calls on a session basis
an interface to make Blackboard REST API calls on a session basis.
"""

# Copyright (C) 2023, Jacob Sánchez Pérez
Expand Down Expand Up @@ -35,12 +35,12 @@
BBAttachment
)

from .exceptions import status_handler

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
_logger.addHandler(logging.NullHandler())


@api_client(timeout=12)
@api_client(timeout=12, status_handler=status_handler)
class BlackboardSession:
"""Represents a user session in Blackboard."""

Expand Down
45 changes: 45 additions & 0 deletions blackboard/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Blackboard REST API exceptions."""

# Copyright (C) 2023, Jacob Sánchez Pérez

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

from typing import Any, NoReturn
from tiny_api_client import APIStatusError


class BBBadRequestError(Exception):
pass


class BBUnauthorizedError(Exception):
pass


class BBForbiddenError(Exception):
pass


def status_handler(status_code: Any) -> NoReturn:
match status_code:
case 400:
raise BBBadRequestError()
case 401:
raise BBUnauthorizedError()
case 403:
raise BBForbiddenError()
case _:
raise APIStatusError(status_code)

0 comments on commit c0c1f55

Please sign in to comment.