Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # 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 | """ |
| 8 | from __future__ import print_function |
| 9 | from __future__ import division |
| 10 | from __future__ import absolute_import |
| 11 | |
| 12 | |
| 13 | class 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 | |
| 51 | class Error(Exception): |
| 52 | """Base class for errors from this module.""" |
| 53 | pass |
| 54 | |
| 55 | |
| 56 | class ActionNotSupported(Error): |
| 57 | """The user is trying to do something we do not support.""" |
| 58 | pass |
| 59 | |
| 60 | |
| 61 | class InputException(Error): |
| 62 | """Error in user input processing.""" |
| 63 | pass |
| 64 | |
| 65 | |
| 66 | class ProjectAlreadyExists(Error): |
| 67 | """Tried to create a project that already exists.""" |
| 68 | |
| 69 | |
| 70 | class FieldDefAlreadyExists(Error): |
| 71 | """Tried to create a custom field that already exists.""" |
| 72 | |
| 73 | |
| 74 | class ComponentDefAlreadyExists(Error): |
| 75 | """Tried to create a component that already exists.""" |
| 76 | |
| 77 | |
| 78 | class NoSuchProjectException(Error): |
| 79 | """No project with the specified name exists.""" |
| 80 | pass |
| 81 | |
| 82 | |
| 83 | class NoSuchTemplateException(Error): |
| 84 | """No template with the specified name exists.""" |
| 85 | pass |
| 86 | |
| 87 | |
| 88 | class NoSuchUserException(Error): |
| 89 | """No user with the specified name exists.""" |
| 90 | pass |
| 91 | |
| 92 | |
| 93 | class NoSuchIssueException(Error): |
| 94 | """The requested issue was not found.""" |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | class NoSuchAttachmentException(Error): |
| 99 | """The requested attachment was not found.""" |
| 100 | pass |
| 101 | |
| 102 | |
| 103 | class NoSuchCommentException(Error): |
| 104 | """The requested comment was not found.""" |
| 105 | pass |
| 106 | |
| 107 | |
| 108 | class NoSuchAmendmentException(Error): |
| 109 | """The requested amendment was not found.""" |
| 110 | pass |
| 111 | |
| 112 | |
| 113 | class NoSuchComponentException(Error): |
| 114 | """No component with the specified name exists.""" |
| 115 | pass |
| 116 | |
| 117 | |
| 118 | class InvalidComponentNameException(Error): |
| 119 | """The component name is invalid.""" |
| 120 | pass |
| 121 | |
| 122 | |
| 123 | class InvalidHotlistException(Error): |
| 124 | """The specified hotlist is invalid.""" |
| 125 | pass |
| 126 | |
| 127 | |
| 128 | class NoSuchFieldDefException(Error): |
| 129 | """No field def for specified project exists.""" |
| 130 | pass |
| 131 | |
| 132 | |
| 133 | class InvalidFieldTypeException(Error): |
| 134 | """Expected field type and actual field type do not match.""" |
| 135 | pass |
| 136 | |
| 137 | |
| 138 | class NoSuchIssueApprovalException(Error): |
| 139 | """The requested approval for the issue was not found.""" |
| 140 | pass |
| 141 | |
| 142 | |
| 143 | class CircularGroupException(Error): |
| 144 | """Circular nested group exception.""" |
| 145 | pass |
| 146 | |
| 147 | |
| 148 | class GroupExistsException(Error): |
| 149 | """Group already exists exception.""" |
| 150 | pass |
| 151 | |
| 152 | |
| 153 | class NoSuchGroupException(Error): |
| 154 | """Requested group was not found exception.""" |
| 155 | pass |
| 156 | |
| 157 | |
| 158 | class 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 | |
| 172 | class PageTokenException(Error): |
| 173 | """Incorrect page tokens.""" |
| 174 | pass |
| 175 | |
| 176 | |
| 177 | class FilterRuleException(Error): |
| 178 | """Violates a filter rule that should show error.""" |
| 179 | pass |
| 180 | |
| 181 | |
| 182 | class OverAttachmentQuota(Error): |
| 183 | """Project will exceed quota if the current operation is allowed.""" |
| 184 | pass |