1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import httplib
18
19
20class Error(Exception):
21    """Base Exception for handlers.
22
23    Attributes:
24        code: HTTP 1.1 status code
25        msg: the W3C names for HTTP 1.1 status code
26    """
27
28    def __init__(self, code=None, msg=None):
29        self.code = code or httplib.INTERNAL_SERVER_ERROR
30        self.msg = msg or httplib.responses[self.code]
31        super(Error, self).__init__(self.code, self.msg)
32
33    def __str__(self):
34        return repr([self.code, self.msg])
35
36
37class FormValidationError(Error):
38    """Form Validtion Exception for handlers."""
39
40    def __init__(self, code=None, msg=None, errors=None):
41        self.code = code or httplib.BAD_REQUEST
42        self.msg = msg or httplib.responses[self.code]
43        self.errors = errors or []
44        super(FormValidationError, self).__init__(self.code, self.msg)
45
46    def __str__(self):
47        return repr([self.code, self.msg, self.errors])
48
49
50class AclError(Error):
51    """Base ACL error."""
52
53    def __init__(self, code):
54        self.code = code
55        self.msg = httplib.responses[code]
56        super(AclError, self).__init__(self.code, self.msg)
57
58    def __str__(self):
59        return repr([self.code, self.msg])
60
61
62class NotFoundError(AclError):
63    """The item being access does not exist."""
64
65    def __init__(self):
66        super(NotFoundError, self).__init__(httplib.NOT_FOUND)
67
68
69class UnauthorizedError(AclError):
70    """The current user is not authenticated."""
71
72    def __init__(self):
73        super(UnauthorizedError, self).__init__(httplib.UNAUTHORIZED)
74
75
76class ForbiddenError(AclError):
77    """The current user does not have proper permission."""
78
79    def __init__(self):
80        super(ForbiddenError, self).__init__(httplib.FORBIDDEN)
81