""" Copyright 2019 Inmanta Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Contact: code@inmanta.com"""fromtypingimportAny,Optionalfromtornadoimportwebfrominmanta.typesimportJsonType
[docs]classBaseHttpException(web.HTTPError):""" A base exception for errors in the server. Classes which extend from the BaseHttpException class cannot have mandatory arguments in their constructor. This is required to determine the status_code of the exception in :meth:`inmanta.protocol.common.MethodProperties._get_http_status_code_for_exception` """def__init__(self,status_code:int=500,message:Optional[str]=None,details:Optional[JsonType]=None)->None:super().__init__(status_code,message)self.details=detailsdefto_body(self)->dict[str,Any]:""" Return a response body """body:JsonType={"message":self.log_message}ifself.detailsisnotNone:body["error_details"]=self.detailsreturnbodydefto_status(self)->int:""" Return the status code """returnself.status_code
[docs]classForbidden(BaseHttpException):""" An exception raised when access is denied (403) """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Access denied"ifmessageisnotNone:msg+=": "+messagesuper().__init__(403,msg,details)
[docs]classUnauthorizedException(BaseHttpException):""" An exception raised when access to this resource is unauthorized """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Access to this resource is unauthorized"ifmessageisnotNone:msg+=": "+messagesuper().__init__(401,msg,details)
[docs]classBadRequest(BaseHttpException):""" This exception is raised for a malformed request """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Invalid request"ifmessageisnotNone:msg+=": "+messagesuper().__init__(400,msg,details)
[docs]classNotFound(BaseHttpException):""" This exception is used to indicate that a request or reference resource was not found. """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Request or referenced resource does not exist"ifmessageisnotNone:msg+=": "+messagesuper().__init__(404,msg,details)
[docs]classConflict(BaseHttpException):""" This exception is used to indicate that a request conflicts with the current state of the resource. """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Request conflicts with the current state of the resource"ifmessageisnotNone:msg+=": "+messagesuper().__init__(409,msg,details)
[docs]classServerError(BaseHttpException):""" An unexpected error occurred in the server """def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="An unexpected error occurred in the server while processing the request"ifmessageisnotNone:msg+=": "+messagesuper().__init__(500,msg,details)
[docs]classShutdownInProgress(BaseHttpException):"""This request can not be fulfilled because the server is going down"""def__init__(self,message:Optional[str]=None,details:Optional[JsonType]=None)->None:msg="Can not complete this request as a shutdown is on progress"ifmessageisnotNone:msg+=": "+messagesuper().__init__(503,msg,details)