SHARE
TWEET

Untitled

a guest Aug 19th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import logging
  2. from django.http import JsonResponse
  3. from core import http_status
  4. from sentry_sdk import capture_exception
  5.  
  6.  
  7. class LoyalManException(Exception):
  8.     def __init__(
  9.         self,
  10.         message: str,
  11.         status_code: int = http_status.HTTP_500_INTERNAL_SERVER_ERROR,
  12.         details=None,
  13.         code: int = None,
  14.         previous=None
  15.     ):
  16.         super().__init__(message)
  17.         self._code = code
  18.         self._status_code = status_code
  19.         self._message = message
  20.         self._details = details
  21.         self._previous = previous
  22.  
  23.         # TODO: fix issues with sentry reporting using sentry_sdk
  24.         sentry_data = {
  25.             "code": self.code,
  26.             "status_code": self.status_code,
  27.             "message": self.message,
  28.             "details": self.details,
  29.             "exception": self.previous
  30.         }
  31.  
  32.         capture_exception(self)
  33.         logging.getLogger("logger").error(self.message, {"extra": sentry_data})
  34.  
  35.     @property
  36.     def code(self) -> int:
  37.         return self._code
  38.  
  39.     @property
  40.     def status_code(self) -> int:
  41.         return self._status_code
  42.  
  43.     @property
  44.     def message(self) -> str:
  45.         return self._message
  46.  
  47.     @property
  48.     def details(self):
  49.         return self._details
  50.  
  51.     @property
  52.     def previous(self):
  53.         return self._previous
  54.  
  55.     @property
  56.     def payload(self) -> dict:
  57.         result = {
  58.             'message': self.message,
  59.         }
  60.  
  61.         if self.code:
  62.             result['code'] = self.code
  63.  
  64.         if self.details:
  65.             result['details'] = self.details
  66.  
  67.         return result
  68.  
  69.     def create_response(self):
  70.         logging.getLogger('logger').error(self.message)
  71.         return JsonResponse(self.payload, status=self.status_code)
  72.  
  73.  
  74. class LoyalManValidationException(Exception):
  75.     def __init__(
  76.         self,
  77.         data: dict,
  78.         status_code: int = http_status.HTTP_400_BAD_REQUEST,
  79.         code: int = None,
  80.         previous=None
  81.     ):
  82.         self._code = code
  83.         self._status_code = status_code
  84.         self._data = data
  85.         self._previous = previous
  86.  
  87.         capture_exception(self)
  88.         logging.getLogger("logger").error(self._data)
  89.  
  90.     @property
  91.     def code(self) -> int:
  92.         return self._code
  93.  
  94.     @property
  95.     def status_code(self) -> int:
  96.         return self._status_code
  97.  
  98.     @property
  99.     def previous(self):
  100.         return self._previous
  101.  
  102.     @property
  103.     def payload(self) -> dict:
  104.         return self._data
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top