|
| 1 | +import re |
| 2 | + |
| 3 | + |
| 4 | +class LabelboxError(Exception): |
| 5 | + """Base class for exceptions.""" |
| 6 | + |
| 7 | + def __init__(self, message, cause=None): |
| 8 | + """ |
| 9 | + Args: |
| 10 | + message (str): Informative message about the exception. |
| 11 | + cause (Exception): The cause of the exception (an Exception |
| 12 | + raised by Python or another library). Optional. |
| 13 | + """ |
| 14 | + super().__init__(message, cause) |
| 15 | + self.message = message |
| 16 | + self.cause = cause |
| 17 | + |
| 18 | + def __str__(self): |
| 19 | + return self.message + str(self.args) |
| 20 | + |
| 21 | + |
| 22 | +class AuthenticationError(LabelboxError): |
| 23 | + """Raised when an API key fails authentication.""" |
| 24 | + |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class AuthorizationError(LabelboxError): |
| 29 | + """Raised when a user is unauthorized to perform the given request.""" |
| 30 | + |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +class ResourceNotFoundError(LabelboxError): |
| 35 | + """Exception raised when a given resource is not found.""" |
| 36 | + |
| 37 | + def __init__(self, db_object_type=None, params=None, message=None): |
| 38 | + """Constructor for the ResourceNotFoundException class. |
| 39 | +
|
| 40 | + Args: |
| 41 | + db_object_type (type): A subtype of labelbox.schema.DbObject. |
| 42 | + params (dict): A dictionary of parameters identifying the sought resource. |
| 43 | + message (str): An optional message to include in the exception. |
| 44 | + """ |
| 45 | + if message is not None: |
| 46 | + super().__init__(message) |
| 47 | + else: |
| 48 | + super().__init__( |
| 49 | + "Resource '%s' not found for params: %r" |
| 50 | + % (db_object_type.type_name(), params) |
| 51 | + ) |
| 52 | + self.db_object_type = db_object_type |
| 53 | + self.params = params |
| 54 | + |
| 55 | + |
| 56 | +class ResourceConflict(LabelboxError): |
| 57 | + """Exception raised when a given resource conflicts with another.""" |
| 58 | + |
| 59 | + pass |
| 60 | + |
| 61 | + |
| 62 | +class ValidationFailedError(LabelboxError): |
| 63 | + """Exception raised for when a GraphQL query fails validation (query cost, |
| 64 | + etc.) E.g. a query that is too expensive, or depth is too deep. |
| 65 | + """ |
| 66 | + |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +class InternalServerError(LabelboxError): |
| 71 | + """Nondescript prisma or 502 related errors. |
| 72 | +
|
| 73 | + Meant to be retryable. |
| 74 | +
|
| 75 | + TODO: these errors need better messages from platform |
| 76 | + """ |
| 77 | + |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +class InvalidQueryError(LabelboxError): |
| 82 | + """Indicates a malconstructed or unsupported query (either by GraphQL in |
| 83 | + general or by Labelbox specifically). This can be the result of either client |
| 84 | + or server side query validation.""" |
| 85 | + |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +class UnprocessableEntityError(LabelboxError): |
| 90 | + """Indicates that a resource could not be created in the server side |
| 91 | + due to a validation or transaction error""" |
| 92 | + |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | +class ResourceCreationError(LabelboxError): |
| 97 | + """Indicates that a resource could not be created in the server side |
| 98 | + due to a validation or transaction error""" |
| 99 | + |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +class NetworkError(LabelboxError): |
| 104 | + """Raised when an HTTPError occurs.""" |
| 105 | + |
| 106 | + def __init__(self, cause): |
| 107 | + super().__init__(str(cause), cause) |
| 108 | + self.cause = cause |
| 109 | + |
| 110 | + |
| 111 | +class TimeoutError(LabelboxError): |
| 112 | + """Raised when a request times-out.""" |
| 113 | + |
| 114 | + pass |
| 115 | + |
| 116 | + |
| 117 | +class InvalidAttributeError(LabelboxError): |
| 118 | + """Raised when a field (name or Field instance) is not valid or found |
| 119 | + for a specific DB object type.""" |
| 120 | + |
| 121 | + def __init__(self, db_object_type, field): |
| 122 | + super().__init__( |
| 123 | + "Field(s) '%r' not valid on DB type '%s'" |
| 124 | + % (field, db_object_type.type_name()) |
| 125 | + ) |
| 126 | + self.db_object_type = db_object_type |
| 127 | + self.field = field |
| 128 | + |
| 129 | + |
| 130 | +class ApiLimitError(LabelboxError): |
| 131 | + """Raised when the user performs too many requests in a short period |
| 132 | + of time.""" |
| 133 | + |
| 134 | + pass |
| 135 | + |
| 136 | + |
| 137 | +class MalformedQueryException(Exception): |
| 138 | + """Raised when the user submits a malformed query.""" |
| 139 | + |
| 140 | + pass |
| 141 | + |
| 142 | + |
| 143 | +class UuidError(LabelboxError): |
| 144 | + """Raised when there are repeat Uuid's in bulk import request.""" |
| 145 | + |
| 146 | + pass |
| 147 | + |
| 148 | + |
| 149 | +class InconsistentOntologyException(Exception): |
| 150 | + pass |
| 151 | + |
| 152 | + |
| 153 | +class MALValidationError(LabelboxError): |
| 154 | + """Raised when user input is invalid for MAL imports.""" |
| 155 | + |
| 156 | + pass |
| 157 | + |
| 158 | + |
| 159 | +class OperationNotAllowedException(Exception): |
| 160 | + """Raised when user does not have permissions to a resource or has exceeded usage limit""" |
| 161 | + |
| 162 | + pass |
| 163 | + |
| 164 | + |
| 165 | +class OperationNotSupportedException(Exception): |
| 166 | + """Raised when sdk does not support requested operation""" |
| 167 | + |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | +class ConfidenceNotSupportedException(Exception): |
| 172 | + """Raised when confidence is specified for unsupported annotation type""" |
| 173 | + |
| 174 | + |
| 175 | +class CustomMetricsNotSupportedException(Exception): |
| 176 | + """Raised when custom_metrics is specified for unsupported annotation type""" |
| 177 | + |
| 178 | + |
| 179 | +class ProcessingWaitTimeout(Exception): |
| 180 | + """Raised when waiting for the data rows to be processed takes longer than allowed""" |
| 181 | + |
| 182 | + |
| 183 | +def error_message_for_unparsed_graphql_error(error_string: str) -> str: |
| 184 | + """ |
| 185 | + Since our client only parses certain graphql errors, this function is used to |
| 186 | + extract the error message from the error string when the error is not |
| 187 | + parsed by the client. |
| 188 | + """ |
| 189 | + # Regex to find the message content |
| 190 | + pattern = r"'message': '([^']+)'" |
| 191 | + # Search for the pattern in the error string |
| 192 | + match = re.search(pattern, error_string) |
| 193 | + if match: |
| 194 | + error_content = match.group(1) |
| 195 | + else: |
| 196 | + error_content = "Unknown error" |
| 197 | + |
| 198 | + return error_content |
0 commit comments