errors.py 1.57 KB
Newer Older
Michal Pavlík's avatar
Michal Pavlík committed
1

Michal Pavlík's avatar
Michal Pavlík committed
2
class ProjectException(Exception):
Michal Pavlík's avatar
Michal Pavlík committed
3 4

    def get_message(self):
Michal Pavlík's avatar
Michal Pavlík committed
5
        return 'Unspecified message'
Michal Pavlík's avatar
Michal Pavlík committed
6 7

    def get_code(self):
Michal Pavlík's avatar
Michal Pavlík committed
8
        return 0
Michal Pavlík's avatar
Michal Pavlík committed
9 10 11 12 13

    def get_http_code(self):
        return 500


Michal Pavlík's avatar
Michal Pavlík committed
14
class PostNotFound(ProjectException):
Michal Pavlík's avatar
Michal Pavlík committed
15 16 17 18
    def __init__(self, post_id):
        self.post_id = post_id

    def get_message(self):
Michal Pavlík's avatar
Michal Pavlík committed
19
        return f'Post with id {self.post_id} doesn\'t exist'
Michal Pavlík's avatar
Michal Pavlík committed
20 21 22 23 24 25

    def get_code(self):
        return 100

    def get_http_code(self):
        return 404
Michal Pavlík's avatar
Michal Pavlík committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78


class UserNotFound(ProjectException):
    def __init__(self, user_id):
        self.user_id = user_id

    def get_message(self):
        return f"User with id {self.user_id} doesn't exist."

    def get_code(self):
        return 200

    def get_http_code(self):
        return 404


class RegistrationFailed(ProjectException):
    def __init__(self, reason):
        self.reason = reason

    def get_message(self):
        return f"Failed to register new user, reason: {self.reason}"

    def get_code(self):
        return 201

    def get_http_code(self):
        return 422


class UnsupportedFileType(ProjectException):
    def __init__(self, filetype):
        self.filetype = filetype

    def get_message(self):
        return f"Files with the extension {self.filetype} aren't allowed."

    def get_code(self):
        return 300

    def get_http_code(self):
        return 412


class WrongCredentials(ProjectException):
    def get_message(self):
        return "The username or password you provided are incorrect."

    def get_code(self):
        return 202

    def get_http_code(self):
        return 401