blob: 51c99519fa19e2ed716336e980429684cbcf10bc [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2017 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Exception classes used throughout monorail.
7"""
8from __future__ import print_function
9from __future__ import division
10from __future__ import absolute_import
11
12
13class ErrorAggregator():
14 """Class for holding errors and raising an exception for many."""
15
16 def __init__(self, exc_type):
17 # type: (type) -> None
18 self.exc_type = exc_type
19 self.error_messages = []
20
21 def __enter__(self):
22 return self
23
24 def __exit__(self, exc_type, exc_value, exc_traceback):
25 # If no exceptions were raised within the context, we check
26 # if any error messages were accumulated that we should raise
27 # an exception for.
28 if exc_type == None:
29 self.RaiseIfErrors()
30 # If there were exceptions raised within the context, we do
31 # nothing to suppress them.
32
33 def AddErrorMessage(self, message, *args, **kwargs):
34 # type: (str, *Any, **Any) -> None
35 """Add a new error message.
36
37 Args:
38 message: An error message, to be formatted using *args and **kwargs.
39 *args: passed in to str.format.
40 **kwargs: passed in to str.format.
41 """
42 self.error_messages.append(message.format(*args, **kwargs))
43
44 def RaiseIfErrors(self):
45 # type: () -> None
46 """If there are errors, raise one exception."""
47 if self.error_messages:
48 raise self.exc_type("\n".join(self.error_messages))
49
50
51class Error(Exception):
52 """Base class for errors from this module."""
53 pass
54
55
56class ActionNotSupported(Error):
57 """The user is trying to do something we do not support."""
58 pass
59
60
61class InputException(Error):
62 """Error in user input processing."""
63 pass
64
65
66class ProjectAlreadyExists(Error):
67 """Tried to create a project that already exists."""
68
69
70class FieldDefAlreadyExists(Error):
71 """Tried to create a custom field that already exists."""
72
73
74class ComponentDefAlreadyExists(Error):
75 """Tried to create a component that already exists."""
76
77
78class NoSuchProjectException(Error):
79 """No project with the specified name exists."""
80 pass
81
82
83class NoSuchTemplateException(Error):
84 """No template with the specified name exists."""
85 pass
86
87
88class NoSuchUserException(Error):
89 """No user with the specified name exists."""
90 pass
91
92
93class NoSuchIssueException(Error):
94 """The requested issue was not found."""
95 pass
96
97
98class NoSuchAttachmentException(Error):
99 """The requested attachment was not found."""
100 pass
101
102
103class NoSuchCommentException(Error):
104 """The requested comment was not found."""
105 pass
106
107
108class NoSuchAmendmentException(Error):
109 """The requested amendment was not found."""
110 pass
111
112
113class NoSuchComponentException(Error):
114 """No component with the specified name exists."""
115 pass
116
117
118class InvalidComponentNameException(Error):
119 """The component name is invalid."""
120 pass
121
122
123class InvalidHotlistException(Error):
124 """The specified hotlist is invalid."""
125 pass
126
127
128class NoSuchFieldDefException(Error):
129 """No field def for specified project exists."""
130 pass
131
132
133class InvalidFieldTypeException(Error):
134 """Expected field type and actual field type do not match."""
135 pass
136
137
138class NoSuchIssueApprovalException(Error):
139 """The requested approval for the issue was not found."""
140 pass
141
142
143class CircularGroupException(Error):
144 """Circular nested group exception."""
145 pass
146
147
148class GroupExistsException(Error):
149 """Group already exists exception."""
150 pass
151
152
153class NoSuchGroupException(Error):
154 """Requested group was not found exception."""
155 pass
156
157
158class InvalidExternalIssueReference(Error):
159 """Improperly formatted external issue reference.
160
161 External issue references must be of the form:
162
163 $tracker_shortname/$tracker_specific_id
164
165 For example, issuetracker.google.com issues:
166
167 b/123456789
168 """
169 pass
170
171
172class PageTokenException(Error):
173 """Incorrect page tokens."""
174 pass
175
176
177class FilterRuleException(Error):
178 """Violates a filter rule that should show error."""
179 pass
180
181
182class OverAttachmentQuota(Error):
183 """Project will exceed quota if the current operation is allowed."""
184 pass