Change "cannot connect to redid" to be a raised exception

This commit is contained in:
Joseph Schorr 2014-03-28 14:20:06 -04:00
parent 40e81f478f
commit 795de4235d
2 changed files with 14 additions and 4 deletions

View file

@ -1,6 +1,8 @@
import redis import redis
import json import json
class BuildStatusRetrievalError(Exception):
pass
class BuildLogs(object): class BuildLogs(object):
ERROR = 'error' ERROR = 'error'
@ -45,7 +47,7 @@ class BuildLogs(object):
log_entries = self._redis.lrange(self._logs_key(build_id), start_index, -1) log_entries = self._redis.lrange(self._logs_key(build_id), start_index, -1)
return (llen, (json.loads(entry) for entry in log_entries)) return (llen, (json.loads(entry) for entry in log_entries))
except redis.ConnectionError: except redis.ConnectionError:
return (0, []) raise BuildStatusRetrievalError('Cannot retrieve build logs')
@staticmethod @staticmethod
def _status_key(build_id): def _status_key(build_id):
@ -65,6 +67,6 @@ class BuildLogs(object):
try: try:
fetched = self._redis.get(self._status_key(build_id)) fetched = self._redis.get(self._status_key(build_id))
except redis.ConnectionError: except redis.ConnectionError:
return None raise BuildStatusRetrievalError('Cannot retrieve build status')
return json.loads(fetched) if fetched else None return json.loads(fetched) if fetched else None

View file

@ -11,6 +11,7 @@ from endpoints.common import start_build
from endpoints.trigger import BuildTrigger from endpoints.trigger import BuildTrigger
from data import model from data import model
from auth.permissions import ModifyRepositoryPermission from auth.permissions import ModifyRepositoryPermission
from data.buildlogs import BuildStatusRetrievalError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -48,7 +49,11 @@ def trigger_view(trigger):
def build_status_view(build_obj, can_write=False): def build_status_view(build_obj, can_write=False):
try:
status = build_logs.get_status(build_obj.uuid) status = build_logs.get_status(build_obj.uuid)
except BuildStatusRetrievalError:
status = None
logger.debug('Can write: %s job_config: %s', can_write, build_obj.job_config) logger.debug('Can write: %s job_config: %s', can_write, build_obj.job_config)
resp = { resp = {
'id': build_obj.uuid, 'id': build_obj.uuid,
@ -169,7 +174,10 @@ class RepositoryBuildLogs(RepositoryParamResource):
start = int(request.args.get('start', 0)) start = int(request.args.get('start', 0))
try:
count, logs = build_logs.get_log_entries(build.uuid, start) count, logs = build_logs.get_log_entries(build.uuid, start)
except BuildStatusRetrievalError:
count, logs = (0, [])
response_obj.update({ response_obj.update({
'start': start, 'start': start,