play-co / hookbox

HookBox is a Comet server and message queue that tightly integrates with your existing web application via web hooks and a REST interface.
http://hookbox.org
MIT License
284 stars 23 forks source link

500 error when publishing to channel that doesn't exist #75

Open timc3 opened 14 years ago

timc3 commented 14 years ago

Shouldn't this be handled by a 404 or just making it more graceful. Was also wondering whether the code should look like this:

class HookboxWebAPI(object):
    logger = logging.getLogger('HookboxRest')
def __init__(self, api):
    self.api = api

def __call__(self, environ, start_response):
    path = environ['PATH_INFO']
    handler = getattr(self, 'render_' + path[1:], None)
    if not handler:
        start_response('404 Not Found', ())
        return "Not Found"
    if not self.api.is_enabled():
        start_response('200 Ok', ())
        return json.dumps([False, { 'msg': "Rest api is disabled by configuration. (Please supply --rest-secret/-r option at start)" }])

    try:
        form = get_form(environ)
        secret = form.pop('security_token', None)
        self.api.authorize(secret)
        return handler(form, start_response)
    except ExpectedException, e:
        start_response('200 Ok', [])
        self.logger.warn('REST Error: %s' % e)
        return json.dumps([False, {'msg': str(e) }])
    except Exception, e:
        self.logger.warn('REST Error: %s', path, exc_info=True)
        start_response('500 Internal server error', [])
        return json.dumps([False, {'msg': str(e) }])

Thanks.