Exemplo n.º 1
0
def handle_exceptions(exc):

    if ERRORS.get(exc.__class__.__name__):
        return error_response(exc.__class__.__name__, exc)

    elif isinstance(exc, HTTPException):
        message = exc.get_description(request.environ)
        message = message.replace('<p>', '').replace('</p>', '')
        body = {
            'status_code': exc.code,
            'error': exc.name,
            'message': message
        }
        headers = exc.get_headers(request.environ)

    else:

        body = {
            'status_code': 500,
            'error': exc.__class__.__name__,
            'message': format_exc()
        }
        headers = {}

    return jsonify(body, status=body.get('status_code'),
                   headers=headers)
Exemplo n.º 2
0
def handle_exceptions(exc):

    if ERRORS.get(exc.__class__.__name__):
        return error_response(exc.__class__.__name__, exc)

    elif isinstance(exc, HTTPException):
        message = exc.get_description(request.environ)
        message = message.replace('<p>', '').replace('</p>', '')
        body = {
            'status_code': exc.code,
            'error': exc.name,
            'message': message
        }
        headers = exc.get_headers(request.environ)

    else:

        body = {
            'status_code': 500,
            'error': exc.__class__.__name__,
            'message': unicode(exc)
        }
        headers = {}

    return jsonify(body, status=body.get('status_code'),
                   headers=headers)
Exemplo n.º 3
0
    def _format_response(self, resp, err=None):
        """
        Handle all errors + format response.
        """
        if self.raise_errors:
            if err:
                raise err

            # check status codes
            elif resp.status_code not in GOOD_CODES:
                try:
                    return resp.json()
                except:
                    raise ClientError(resp.content)

                err = ERRORS.get(d['error'])
                if not err:
                    raise ClientError(resp.content)
                raise err(d['message'])
            elif resp.status_code == 204:
                return True
            else:
                return resp.json()

        else:
            try:
                return resp.json()
            except:
                return {
                    'error':'InternalServerError',
                    'status_code': 500,
                    'message':getattr(resp, 'content', 'Are you sure the API is running?')
                }
Exemplo n.º 4
0
 def _generate():
     for line in r.iter_lines():
         d = json_to_obj(line)
         # catch errors
         if d.get('error'):
             err = ERRORS.get(d['error'])
             if not err:
                 raise ClientError(d)
             raise err(d['message'])
         yield d
Exemplo n.º 5
0
 def get_status(self, **kw):
     """
     Get a job's status.
     """
     status_url = kw.get('status_url')
     if not status_url:
         raise ClientError(
             'You must pass in the "status_url" returned from a bulk/recipe endpoint.')
     r = requests.get(status_url)
     d = r.json()
     # catch errors
     if d.get('error'):
         err = ERRORS.get(d['error'])
         if not err:
             raise ClientError(d)
         raise err(d['message'])
     return d
Exemplo n.º 6
0
    def _handle_errors(self, resp, err=None):
        """
        Handle all errors.
        """
        if err:
            raise err

        # check status codes
        elif resp.status_code not in GOOD_CODES:
            try:
                d = resp.json()
            except:
                raise ClientError(resp.content)

            err = ERRORS.get(d['error'])
            if not err:
                raise ClientError(resp.content)
            raise err(d['message'])
Exemplo n.º 7
0
    def _stream(self, r):
        for line in r.iter_lines():
            d = json_to_obj(line)

            # catch bad responses:
            if not isinstance(d, dict):
                e = 'InternalServerError'
                err_msg = 'Invalid Response: {}'.format(d)
                if self._raise_errors:
                    raise ERRORS[e](err_msg)
                yield {'status_code': 500, "error": e, "message": err_msg}

            # catch errors
            elif d.get('error'):
                if self._raise_errors:
                    err = ERRORS.get(d['error'])
                    if not err:
                        raise ClientError(d)
                    raise err(d['message'])
            yield d
Exemplo n.º 8
0
    def _format_response(self, resp, err=None):
        """
        Handle all errors + format response.
        """
        if self._raise_errors:
            if err:
                raise err

            # check status codes
            if resp.status_code not in GOOD_CODES:
                try:
                    d = resp.json()
                except:
                    raise ClientError('Response could not be parsed.')
                err = ERRORS.get(d['error'])
                if not err:
                    raise ClientError(resp.content)
                raise err(d['message'])

            elif resp.status_code == 204:
                return True
            else:
                return resp.json()

        else:
            if resp is None:
                return {
                    'error': 'InternalServerError',
                    'status_code': 500,
                    'message': 'Are you sure the API is running?'
                }
            if resp.status_code == 204:
                return True
            try:
                return resp.json()
            except:
                return {
                    'error': 'InternalServerError',
                    'status_code': 500,
                    'message': getattr(resp, 'text', 'Are you sure the API is running?')
                }
Exemplo n.º 9
0
    def execute(self, query, **kw):
        """
        Execute a sql command and stream resutls.
        """
        # merge in api key
        kw.update({'apikey': self.apikey})
        url = self._format_url('sql')

        # post request
        r = requests.post(url, params=kw, data={'query': query})

        # stream results.
        for line in r.iter_lines():
            d = json_to_obj(line)

            # catch errors
            if d.get('error'):
                err = ERRORS.get(d['error'])
                if not err:
                    raise ClientError(d)
                raise err(d['message'])
            yield d