exceptions.py 2.23 KB
Newer Older
1
"""Exceptions raised by the credit API. """
Clinton Blackburn committed
2
from __future__ import unicode_literals
3

Clinton Blackburn committed
4 5 6 7
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.exceptions import APIException

8

Clinton Blackburn committed
9
# TODO: Cleanup this mess! ECOM-2908
10 11


12 13 14 15 16 17 18 19 20
class CreditApiBadRequest(Exception):
    """
    Could not complete a request to the credit API because
    there was a problem with the request (as opposed to an internal error).
    """
    pass


class InvalidCreditRequirements(CreditApiBadRequest):
21
    """
22
    The requirement dictionary provided has invalid format.
23
    """
24 25 26
    pass


27
class InvalidCreditCourse(CreditApiBadRequest):
28
    """
29 30 31 32 33
    The course is not configured for credit.
    """
    pass


34
class UserIsNotEligible(CreditApiBadRequest):
35 36 37 38 39 40
    """
    The user has not satisfied eligibility requirements for credit.
    """
    pass


41 42 43 44 45 46 47 48
class CreditProviderNotConfigured(CreditApiBadRequest):
    """
    The requested credit provider is not configured correctly for the course.
    """
    pass


class RequestAlreadyCompleted(CreditApiBadRequest):
49 50 51 52 53 54
    """
    The user has already submitted a request and received a response from the credit provider.
    """
    pass


55
class CreditRequestNotFound(CreditApiBadRequest):
56 57 58 59 60 61
    """
    The request does not exist.
    """
    pass


62
class InvalidCreditStatus(CreditApiBadRequest):
63 64
    """
    The status is not either "approved" or "rejected".
65
    """
66
    pass
Clinton Blackburn committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


class InvalidCreditRequest(APIException):
    """ API request is invalid. """
    status_code = status.HTTP_400_BAD_REQUEST


class UserNotEligibleException(InvalidCreditRequest):
    """ User not eligible for credit for a given course. """

    def __init__(self, course_key, username):
        detail = _('[{username}] is not eligible for credit for [{course_key}].').format(username=username,
                                                                                         course_key=course_key)
        super(UserNotEligibleException, self).__init__(detail)


class InvalidCourseKey(InvalidCreditRequest):
    """ Course key is invalid. """

    def __init__(self, course_key):
        detail = _('[{course_key}] is not a valid course key.').format(course_key=course_key)
        super(InvalidCourseKey, self).__init__(detail)