コード例 #1
0
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception
        """
        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:
            self.app.logger.exception("Internal Error")

        if code == 404:
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path, rules.keys())
            if close_matches:
                data['message'] += '. You have requested this URI [' + request.path + \
                                   '] but did you mean ' + \
                                   ' or '.join((rules[match]
                                   for match in close_matches)) + ' ?'

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #2
0
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception

        """
        got_request_exception.send(self.app, exception=e)

        if not hasattr(e, 'code') and self.app.propagate_exceptions:
            exc_type, exc_value, tb = sys.exc_info()
            if exc_value is e:
                exc = exc_type(exc_value)
                exc.__traceback__ = tb
                raise exc
            else:
                raise e

        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:

            # There's currently a bug in Python3 that disallows calling
            # logging.exception() when an exception hasn't actually be raised
            if sys.exc_info() == (None, None, None):
                self.app.logger.error("Internal Error")
            else:
                self.app.logger.exception("Internal Error")

        if code == 404 and ('message' not in data
                            or data['message'] == HTTP_STATUS_CODES[404]):
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path,
                                                      rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] += ". "
                else:
                    data["message"] = ""

                data['message'] += 'You have requested this URI [' + request.path + \
                        '] but did you mean ' + \
                        ' or '.join((rules[match]
                                     for match in close_matches)) + ' ?'

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(
                resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #3
0
ファイル: __init__.py プロジェクト: silasray/flask-restful
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception

        """
        got_request_exception.send(self.app, exception=e)

        if not hasattr(e, 'code') and self.app.propagate_exceptions:
            exc_type, exc_value, tb = sys.exc_info()
            if exc_value is e:
                exc = exc_type(exc_value)
                exc.__traceback__ = tb
                raise exc
            else:
                raise e

        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:

            # There's currently a bug in Python3 that disallows calling
            # logging.exception() when an exception hasn't actually be raised
            if sys.exc_info() == (None, None, None):
                self.app.logger.error("Internal Error")
            else:
                self.app.logger.exception("Internal Error")

        if code == 404 and ('message' not in data or
                            data['message'] == HTTP_STATUS_CODES[404]):
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path, rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] += ". "
                else:
                    data["message"] = ""

                data['message'] += 'You have requested this URI [' + request.path + \
                        '] but did you mean ' + \
                        ' or '.join((rules[match]
                                     for match in close_matches)) + ' ?'

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #4
0
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception

        """
        got_request_exception.send(self, exception=e)

        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:
            self.app.logger.exception("Internal Error")

        if code == 404 and ('message' not in data
                            or data['message'] == HTTP_STATUS_CODES[404]):
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path,
                                                      rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] += ". "
                else:
                    data["message"] = ""

                data['message'] += 'You have requested this URI [' + request.path + \
                        '] but did you mean ' + \
                        ' or '.join((rules[match]
                                     for match in close_matches)) + ' ?'

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(
                resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #5
0
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception
        """
        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:
            self.app.logger.exception("Internal Error")

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #6
0
ファイル: __init__.py プロジェクト: andrew-d/flask-restful
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception

        """
        got_request_exception.send(self, exception=e)

        code = getattr(e, 'code', 500)
        data = getattr(e, 'data', error_data(code))

        if code >= 500:
            self.app.logger.exception("Internal Error")

        if code == 404 and ('message' not in data or
                            data['message'] == HTTP_STATUS_CODES[404]):
            rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
                          for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path, rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] += ". "
                else:
                    data["message"] = ""

                data['message'] += 'You have requested this URI [' + request.path + \
                        '] but did you mean ' + \
                        ' or '.join((rules[match]
                                     for match in close_matches)) + ' ?'

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(resp,
                self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #7
0
ファイル: __init__.py プロジェクト: kevinburke/flask-restful
    def handle_error(self, e):
        """Error handler for the API transforms a raised exception into a Flask
        response, with the appropriate HTTP status code and body.

        :param e: the raised Exception object
        :type e: Exception
        """
        code = getattr(e, "code", 500)
        data = getattr(e, "data", error_data(code))

        if code >= 500:
            self.app.logger.exception("Internal Error")

        if code == 404:
            rules = dict([(re.sub("(<.*>)", "", rule.rule), rule.rule) for rule in self.app.url_map.iter_rules()])
            close_matches = difflib.get_close_matches(request.path, rules.keys())
            if close_matches:
                # If we already have a message, add punctuation and continue it.
                if "message" in data:
                    data["message"] += ". "
                else:
                    data["message"] = ""
                data["message"] += (
                    "You have requested this URI ["
                    + request.path
                    + "] but did you mean "
                    + " or ".join((rules[match] for match in close_matches))
                    + " ?"
                )

        resp = self.make_response(data, code)

        if code == 401:
            resp = unauthorized(resp, self.app.config.get("HTTP_BASIC_AUTH_REALM", "flask-restful"))

        return resp
コード例 #8
0
ファイル: test_api.py プロジェクト: aldenm/flask-restful
 def test_unauthorized_custom_realm(self):
     response = Mock()
     response.headers = {}
     unauthorized(response, realm='Foo')
     self.assertEquals(response.headers['WWW-Authenticate'], 'Basic realm="Foo"')
コード例 #9
0
ファイル: test_api.py プロジェクト: aldenm/flask-restful
 def test_unauthorized(self):
     response = Mock()
     response.headers = {}
     unauthorized(response, "flask-restful")
     self.assertEquals(response.headers['WWW-Authenticate'],
                   'Basic realm="flask-restful"')
コード例 #10
0
ファイル: test_api.py プロジェクト: theorm/flask-restful
 def test_unauthorized_custom_realm(self):
     response = Mock()
     response.headers = {}
     unauthorized(response, realm='Foo')
     self.assertEquals(response.headers['WWW-Authenticate'],
                       'Basic realm="Foo"')
コード例 #11
0
ファイル: test_api.py プロジェクト: theorm/flask-restful
 def test_unauthorized(self):
     response = Mock()
     response.headers = {}
     unauthorized(response, "flask-restful")
     self.assertEquals(response.headers['WWW-Authenticate'],
                       'Basic realm="flask-restful"')