def wrap_query(querier, sq, varenv=None, transaction_id=None): """ Run a query with the given querier (usually something like ctx.low_querier.read) - performing appropriate envelope packing and unpacking, multiple queries, error handling, etc """ LOG.error( 'deprecated', 'mw.mql.pathexpr.wrap_query() is DEPRECATED and will go away soon!') if isinstance(sq, basestring): # convert to json query try: # XXX should eventually use unicode, for now utf8 sq = json.loads(sq, encoding='utf-8', result_encoding='utf-8') except ValueError, e: # debug ME-907 LOG.exception('mql.pathexpr.wrap_query()', sq=sq, varenv=varenv) SIMPLEJSON_ERR_RE = re.compile('^(.+): line (\d+) column (\d+)') m = SIMPLEJSON_ERR_RE.match(str(e)) if not m: raise response = JSONResponse(status='400 Bad Request', code='/api/status/error/request') text = 'json parse error: ' + m.group(1) response.log(text, line=int(m.group(2)), column=int(m.group(3)), level='error') return response.response except Exception, e: return json_traceback(exception=e, status='400 Bad Request', code='/api/status/error/request')
def __init__(self, msg, http_code=400, app_code=DEF_ME_CODE, inner_exc=None, **kwds): self.msg = msg Exception.__init__(self, msg) if not is_valid_HTTP_code(http_code): http_code = 500 self.http_status = get_HTTP_err(http_code) self.http_code = http_code # app_code and and api code setup codes = app_code.split('/') if len(codes) < 3: codes = self.DEF_ME_CODE.split('/') self.comp_code = '%s/%s' % (self.DEF_PFX, codes[1]) self.app_code = '%s' % '/'.join(codes[2:]) self.messages = [self.gen_msgs(**kwds)] if not kwds.has_key('error'): # don't extract the current frame (__init__) stack = traceback.extract_stack()[:-1] kwds['traceback'] = '\r\n'.join(traceback.format_list(stack)) # log inner exception or self exc = self if inner_exc: exc = inner_exc comp = app_code[1:].replace('/', '.') if exc == self: LOG.debug(comp, msg, **kwds) else: LOG.exception(msg, **kwds) self.kwds = kwds
except EmptyResult, e: LOG.info('emptyresult', '%s' % e) response.log('empty result for query %s' % subq) result = None # exceptions should be packed into response['error'] except ParameterizedError, e: if isinstance(e, MQLInternalError): response.extend(status='500 Internal Server Error') else: response.extend(status='400 Bad Request') tb = json_traceback(response=response, exception=e) response.log('parse exception: %s' % e, level='error') result = None except Exception, e: LOG.exception('python.exception') tb = json_traceback(response=response, exception=e) return tb response.extend(result=results) if 'cursor' in varenv: response.extend(cursor=varenv['cursor']) return response.response except Exception, e: LOG.exception('python.exception') return json_traceback(response=response, exception=e)