def _failed(e): err = HTTPError() err.status_int = code body = StringIO() pprint(str(e), body) body.seek(0) err.body = body.read() raise err
def _check_type(self, type_, value, code): # XXX we should keep the value converted, no ? if type_ in self.types: try: self.types[type_](value) except ValueError, e: err = HTTPError() err.status_int = code body = StringIO() pprint(str(e), body) body.seek(0) err.body = body.read() raise err
def raise_for_status(response): if response.status_code >= 400: raise HTTPError( detail=response.status, headers=response.headers, comment=response.body )
def create_request_from_session(url, session, timeout=DEFAULT_TIMEOUT, verify=True): try: # Use session to follow redirects: with closing( session.head(url, allow_redirects=True, timeout=timeout, verify=verify)) as head: req = Request.blank(head.url) req.environ['webob.client.timeout'] = timeout # Get cookies from head: cookies_dict = head.cookies.get_dict() # Set request cookies to the head cookies: req.headers['Cookie'] = ','.join(name + '=' + cookies_dict[name] for name in cookies_dict) # Set the headers to the session headers: for item in head.request.headers: req.headers[item] = head.request.headers[item] return req except (MissingSchema, InvalidSchema): # Missing schema can occur in tests when the url # is not pointing to any resource. Simply pass. req = Request.blank(url) req.environ['webob.client.timeout'] = timeout return req except Timeout: raise HTTPError('Timeout')
def prepare(self, context): """ Setup basic stuff needed for all pages """ context.__dict__['whatsnewdays'] = self.whatsnewdays context.__dict__['lastplay_count'] = self.lastplay_count try: djrow = context.db.lastplay.query(DJs).filter( DJs.dj == context.djname).one() except NoResultFound: raise HTTPNotFound('Host Not Found') context.__dict__['djname'] = djrow.dj package = 'web.app.djrq.model.' + djrow.databasetype context.__dict__['databasetype'] = djrow.databasetype context.__dict__['queries'] = load(package + '.queries:Queries')( db=context.db.default) Listeners = load(package + '.listeners:Listeners') context.__dict__['Users'] = load(package + '.users:Users') try: album = load(package + '.album:Album', default='Album') except ImportError: album = 'Album' try: artist = load(package + '.artist:Artist', default='Artist') except ImportError: artist = 'Artist' context.__dict__['artist'] = artist context.__dict__['album'] = album context.__dict__['requestlist'] = load(package + '.requestlist:RequestList') context.__dict__['mistags'] = load(package + '.mistags:Mistags') context.__dict__['suggestions'] = load(package + '.suggestions:Suggestions') if context.queries.db is None: raise HTTPError("Queries is None!") context.__dict__['dbstats'] = context.queries.get_song_stats() context.__dict__['requests_info'] = context.queries.get_requests_info() context.__dict__['new_counts'] = context.queries.get_new_counts( days=context.whatsnewdays) try: context.__dict__['listeners'] = context.db.default.query( Listeners).one() except NoResultFound: context.__dict__['listeners'] = None context.__dict__['alldjs'] = context.db.lastplay.query(DJs).filter( DJs.hide_from_menu == 0).order_by(DJs.dj) context.siteoptions = context.queries.get_siteoptions() context.suggestions_count = context.queries.get_suggestions_count( ).suggestions_count context.mistags_count = context.queries.get_mistags_count( ).mistags_count
def raise_for_status(response): # Raise error if status is above 300: if response.status_code >= 400: raise HTTPError(detail=response.status + '\n' + response.text, headers=response.headers, comment=response.body) elif response.status_code >= 300: try: text = response.text except AttributeError: # With this status_code, response.text could # be ill-defined. If the redirect does not set # an encoding (i.e. response.charset is None). # Set the text to empty string: text = '' raise HTTPError( detail=(response.status + '\n' + text + '\n' + 'This is redirect error. These should not usually raise ' + 'an error in pydap beacuse redirects are handled ' + 'implicitly. If it failed it is likely due to a ' + 'circular redirect.'), headers=response.headers, comment=response.body)
def __call__(self, request): for fn in self.PRE_INTERCEPTOR: request = fn(request) for router in self._ROUTERS: response = router.match(request) for fn in self.POST_INTERCEPTOR: response = fn(request, response) if response: return response raise HTTPError('<h1>wrong not page</h1>')
def dataset(self): # auth session try: return self._open_url() except HTTPError as e: # I need the 500 because pydap re-raises HTTPError wihout setting the code if not (e.code != 400 or e.code != 300 or e.code != 500): raise e # Check Url (probably inefficient..., but worth a try to get authenticated) try: self.session.get(self.source + ".dds") return self._open_url() except HTTPError as e: if e.code != 400: raise e _logger.exception("Error opening PyDap url '%s'" % self.source) raise HTTPError("Could not open PyDap url '%s'.\nCheck login credentials." % self.source)
def __init__(self, code, reason=None, **data): self.code = code data["reason"] = self.explanation = reason or "UNKNOWN_ERROR" self.error_data = data HTTPError.__init__(self)