Пример #1
0
 def __init__(self,
              message=None,
              logMessage=None,
              status=None,
              logResource=None,
              logResourceMethod=None,
              verb=None,
              url=None,
              logPayload=None,
              logHeaders=None,
              context=None):
     self.timeStamp = DateTimeHelper.now()
     self.status = HttpStatus.map(DEFAULT_STATUS if ObjectHelper.
                                  isNone(status) else status).enumValue
     self.message = message if ObjectHelper.isNotEmpty(
         message
     ) and StringHelper.isNotBlank(
         message
     ) else DEFAULT_MESSAGE if 500 <= self.status else self.status.enumName
     self.verb = verb if ObjectHelper.isNotNone(
         verb) else self.getRequestVerb()
     self.url = url if ObjectHelper.isNotNone(url) else self.getRequestUrl()
     self.logMessage = DEFAULT_LOG_MESSAGE if ObjectHelper.isNone(
         logMessage) or StringHelper.isBlank(logMessage) else logMessage
     self.logResource = DEFAULT_LOG_RESOURCE if ObjectHelper.isNone(
         logResource) else logResource
     self.logResourceMethod = DEFAULT_LOG_RESOURCE_METHOD if ObjectHelper.isNone(
         logResourceMethod) else logResourceMethod
     self.logPayload = logPayload if ObjectHelper.isNotNone(
         logPayload) else self.getRequestBody()
     self.logHeaders = logHeaders if ObjectHelper.isNotNone(
         logHeaders) else self.getRequestHeaders()
     self.context = HttpDomain.CONTROLLER_CONTEXT if ObjectHelper.isNone(
         context) else context
Пример #2
0
 def getSettingTree(self,
                    settingFilePath=None,
                    defaultSettingFilePath=None,
                    settingTree=None):
     if ObjectHelper.isEmpty(settingTree):
         settingTree = {}
     fallbackSettingFilePath = defaultSettingFilePath if not settingFilePath == defaultSettingFilePath else None
     if ObjectHelper.isNone(settingFilePath) or StringHelper.isBlank(
             settingFilePath
     ) or not EnvironmentHelper.OS.path.isfile(settingFilePath):
         self.failure(
             f'The "{settingFilePath}" setting file path was not found',
             None)
         return self.getSettingTree(settingFilePath=fallbackSettingFilePath,
                                    settingTree=settingTree)
     try:
         settingTree = SettingHelper.getSettingTree(
             settingFilePath,
             fallbackSettingFilePath=fallbackSettingFilePath,
             fallbackSettingTree=settingTree,
             keepDepthInLongString=True)
     except Exception as exception:
         if ObjectHelper.isNone(fallbackSettingFilePath):
             self.error(
                 f'Failed to load setting tree from "{settingFilePath}" setting file path. Returning {settingTree} by default',
                 exception)
         else:
             self.failure(
                 f'Failed to load setting tree from "{settingFilePath}" setting file path and "{fallbackSettingFilePath}" default setting file path. Only setting file path will be loadded now',
                 exception)
             try:
                 settingTree = SettingHelper.getSettingTree(
                     settingFilePath, keepDepthInLongString=True)
             except Exception as exception:
                 self.failure(
                     f'Failed to load setting tree from "{settingFilePath}" setting file path as well. Returning {settingTree} by default',
                     exception)
     return settingTree
def getErrorMessage(clientResponse, exception=None):
    completeErrorMessage = f'{HttpClientConstant.ERROR_AT_CLIENT_CALL_MESSAGE}{c.DOT_SPACE}{HttpClientConstant.CLIENT_DID_NOT_SENT_ANY_MESSAGE}'
    errorMessage = HttpClientConstant.CLIENT_DID_NOT_SENT_ANY_MESSAGE
    possibleErrorMessage = None
    bodyAsJson = {}
    try :
        bodyAsJson = clientResponse.json()
    except Exception as innerException :
        bodyAsJsonException = FlaskUtil.safellyGetResponseJson(clientResponse)
        log.log(getErrorMessage, f'Invalid client response: {bodyAsJsonException}', exception=innerException)
        log.debug(getErrorMessage, f'Not possible to get error message from client response: {bodyAsJsonException}. Proceeding with value {bodyAsJson} by default', exception=innerException, muteStackTrace=True)
    try:
        if ObjectHelper.isNotNone(clientResponse):
            if ObjectHelper.isDictionary(bodyAsJson):
                possibleErrorMessage = bodyAsJson.get('message', bodyAsJson.get('error')).strip()
            if ObjectHelper.isList(bodyAsJson) and 0 < len(bodyAsJson):
                possibleErrorMessage = bodyAsJson[0].get('message', bodyAsJson[0].get('error')).strip()
        if ObjectHelper.isNotNone(possibleErrorMessage) and StringHelper.isNotBlank(possibleErrorMessage):
            errorMessage = f'{c.LOG_CAUSE}{possibleErrorMessage}'
        else:
            log.debug(getErrorMessage, f'Client response {FlaskUtil.safellyGetResponseJson(clientResponse)}')
        exceptionPortion = HttpClientConstant.ERROR_AT_CLIENT_CALL_MESSAGE if ObjectHelper.isNone(exception) or StringHelper.isBlank(exception) else str(exception)
        completeErrorMessage = f'{exceptionPortion}{c.DOT_SPACE}{errorMessage}'
    except Exception as exception:
        log.warning(getErrorMessage, f'Not possible to get error message. Returning {completeErrorMessage} by default', exception=exception)
    return completeErrorMessage
Пример #4
0
def isNoneOrBlank(thing):
    return ObjectHelper.isNone(thing) or StringHelper.isBlank(str(thing))
Пример #5
0
def handleLogErrorException(exception,
                            resourceInstance,
                            resourceInstanceMethod,
                            context,
                            apiInstance=None):
    if not (isinstance(exception, GlobalException)
            or GlobalException.__name__ == exception.__class__.__name__):
        log.debug(
            handleLogErrorException,
            f'Failed to excecute {resourceInstanceMethod.__name__} method due to {exception.__class__.__name__} exception',
            exception=exception)
        message = None
        status = None
        logMessage = None
        if (isinstance(exception, NoAuthorizationError) or
                NoAuthorizationError.__name__ == exception.__class__.__name__
                or isinstance(exception, RevokedTokenError)
                or RevokedTokenError.__name__ == exception.__class__.__name__
                or isinstance(exception, InvalidSignatureError) or
                InvalidSignatureError.__name__ == exception.__class__.__name__
                or isinstance(exception, ExpiredSignatureError)
                or ExpiredSignatureError.__name__
                == exception.__class__.__name__):
            message = 'Unauthorized' if ObjectHelper.isNone(
                exception) or StringHelper.isBlank(
                    str(exception)) else str(exception)
            status = HttpStatus.UNAUTHORIZED
        if ObjectHelper.isNotNone(exception) and StringHelper.isNotBlank(
                str(exception)):
            logMessage = str(exception)
        else:
            logMessage = DEFAULT_LOG_MESSAGE
        exception = GlobalException(message=message,
                                    logMessage=logMessage,
                                    logResource=resourceInstance,
                                    logResourceMethod=resourceInstanceMethod,
                                    status=status)
    try:
        if not context == exception.context:
            exception = GlobalException(
                message=exception.message,
                logMessage=exception.logMessage,
                logResource=resourceInstance,
                logResourceMethod=resourceInstanceMethod,
                status=exception.status,
                context=context)
        else:
            if not exception.logResource or c.NOTHING == exception.logResource or not resourceInstance == exception.logResource:
                exception.logResource = resourceInstance
            if not exception.logResourceMethod or c.NOTHING == exception.logResourceMethod or not resourceInstanceMethod == exception.logResourceMethod:
                exception.logResourceMethod = resourceInstanceMethod
        httpErrorLog = ErrorLog.ErrorLog()
        httpErrorLog.override(exception)
        if ObjectHelper.isNone(apiInstance):
            from python_framework import FlaskManager
            apiInstance = FlaskManager.getApi()
        else:
            apiInstance = apiInstance
            try:
                apiInstance.repository.commit()
            except Exception as preCommitException:
                log.warning(
                    handleLogErrorException,
                    f'Failed to pre commit before persist {ErrorLog.ErrorLog.__name__}',
                    exception=preCommitException)
        apiInstance.repository.saveAndCommit(httpErrorLog)
    except Exception as errorLogException:
        log.warning(handleLogErrorException,
                    f'Failed to persist {ErrorLog.ErrorLog.__name__}',
                    exception=errorLogException)
    return exception