def raiseExceptionIfNeeded(clientResponse): status = FlaskUtil.safellyGetResponseStatus(clientResponse) ###- clientResponse.status_code if ObjectHelper.isNone(status) or 500 <= status: raise GlobalException( logMessage = getErrorMessage(clientResponse), url = FlaskUtil.safellyGetRequestUrlFromResponse(clientResponse), status = status, logHeaders = { 'requestHeaders': FlaskUtil.safellyGetRequestHeadersFromResponse(clientResponse), 'responseHeaders': FlaskUtil.safellyGetResponseHeaders(clientResponse) }, logPayload = { 'requestBody': FlaskUtil.safellyGetRequestJsonFromResponse(clientResponse), 'responseBody': FlaskUtil.safellyGetResponseJson(clientResponse) }, context = HttpDomain.CLIENT_CONTEXT ) elif 400 <= status: raise GlobalException( message = getErrorMessage(clientResponse), logMessage = HttpClientConstant.ERROR_AT_CLIENT_CALL_MESSAGE, url = FlaskUtil.safellyGetRequestUrlFromResponse(clientResponse), status = status, logHeaders = { 'requestHeaders': FlaskUtil.safellyGetRequestHeadersFromResponse(clientResponse), 'responseHeaders': FlaskUtil.safellyGetResponseHeaders(clientResponse) }, logPayload = { 'requestBody': FlaskUtil.safellyGetRequestJsonFromResponse(clientResponse), 'responseBody': FlaskUtil.safellyGetResponseJson(clientResponse) }, context = HttpDomain.CLIENT_CONTEXT )
def handleSecuredControllerMethod(args, kwargs, contentType, resourceInstance, resourceInstanceMethod, contextRequired, apiKeyRequired, roleRequired, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException): contextList = SecurityManager.getContext() if not any(role in set(contextList) for role in roleRequired): raise GlobalException( message='Role not allowed', logMessage= f'''Roles {contextList} trying to access denied resourse. Allowed roles {roleRequired}''', status=HttpStatus.FORBIDDEN) elif ObjectHelper.isNotEmptyCollection(apiKeyRequired): return handleLockedByApiKeyControllerMethod( args, kwargs, contentType, resourceInstance, resourceInstanceMethod, contextRequired, apiKeyRequired, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException) elif ObjectHelper.isNotEmptyCollection(contextRequired): return handleSessionedControllerMethod( args, kwargs, contentType, resourceInstance, resourceInstanceMethod, contextRequired, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException) return handleControllerMethod(args, kwargs, contentType, resourceInstance, resourceInstanceMethod, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException)
def validateBodyAsJson(requestBodyAsJson, requestClass): if ObjectHelper.isNotNone(requestClass): requestBodyAsJsonIsList = ObjectHelper.isList(requestBodyAsJson) requestClassIsList = ObjectHelper.isList( requestClass) and ObjectHelper.isList(requestClass[0]) if not ((requestBodyAsJsonIsList and requestClassIsList) or (not requestBodyAsJsonIsList and not requestClassIsList)): raise GlobalException(message='Bad request', logMessage='Bad request', status=HttpStatus.BAD_REQUEST)
def getRequestBodyAsJson(contentType, requestClass): try: if OpenApiManager.DEFAULT_CONTENT_TYPE == contentType: requestBodyAsJson = FlaskUtil.safellyGetJson() elif OpenApiManager.MULTIPART_X_MIXED_REPLACE in contentType: requestBodyAsJson = FlaskUtil.safellyGetData() else: raise Exception(f'Content type "{contentType}" not implemented') except Exception as exception: raise GlobalException(message='Not possible to parse the request', logMessage=str(exception), status=HttpStatus.BAD_REQUEST) validateBodyAsJson(requestBodyAsJson, requestClass) return requestBodyAsJson
def raiseException(clientResponse, exception): raise GlobalException( logMessage = getErrorMessage(clientResponse, exception=exception), url = FlaskUtil.safellyGetRequestUrlFromResponse(clientResponse), status = FlaskUtil.safellyGetResponseStatus(clientResponse), logHeaders = { 'requestHeaders': FlaskUtil.safellyGetRequestHeadersFromResponse(clientResponse), 'responseHeaders': FlaskUtil.safellyGetResponseHeaders(clientResponse) }, logPayload = { 'requestBody': FlaskUtil.safellyGetRequestJsonFromResponse(clientResponse), 'responseBody': FlaskUtil.safellyGetResponseJson(clientResponse) }, context = HttpDomain.CLIENT_CONTEXT )
def handleSessionedControllerMethod(args, kwargs, contentType, resourceInstance, resourceInstanceMethod, contextRequired, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException): contextList = SessionManager.getContext() if not any(context in set(contextList) for context in contextRequired): raise GlobalException( message='Session not allowed', logMessage= f'''Sessions {contextList} trying to access denied resourse. Allowed contexts: {contextRequired}''', status=HttpStatus.FORBIDDEN) else: return handleControllerMethod(args, kwargs, contentType, resourceInstance, resourceInstanceMethod, requestHeaderClass, requestParamClass, requestClass, logRequest, muteStacktraceOnBusinessRuleException)
def wrapedFunction(*args, **kwargs): try: functionReturn = function(*args, **kwargs) except Exception as exception: if isinstance(exception, GlobalException): raise exception logMessage = str(exception) if StringHelper.isNotBlank( str(exception)) else LOG_MESSAGE_NOT_PRESENT functionName = ReflectionHelper.getName( function, typeName=c.TYPE_FUNCTION) log.wrapper( EncapsulateItWithGlobalException, f'''Failed to execute "{functionName}(args={args}, kwargs={kwargs})" {c.TYPE_FUNCTION} call''', exception) raise GlobalException( message=message, logMessage=logMessage, logResource=ReflectionHelper.getParentClass(function), logResourceMethod=function, status=HttpStatus.map( encapsulateItWithGlobalExceptionStatus).enumValue) return functionReturn