Here are the examples of the python api flask.g.exceptional taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
13 Examples
3
Example 1
Project: flask-exceptional Source File: tests.py
def test_01_exception(self):
"""Test mandatory data requirements for the Exceptional API.
See http://docs.exceptional.io/api/publish for details.
"""
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
exception = data["exception"]
assert "backtrace" in exception
assert "exception_class" in exception
assert "message" in exception
assert "occurred_at" in exception
environment = data["application_environment"]
assert environment[
"application_root_directory"] == self.app.root_path
assert "env" in environment
3
Example 2
Project: flask-exceptional Source File: tests.py
def test_02_http_exception(self):
"""Test logging an HTTP exception.
"""
with self.app.test_client() as client:
client.get("/http/404")
data = json.loads(g.exceptional)
exception = data["exception"]
assert "404" in exception["message"]
3
Example 3
Project: flask-exceptional Source File: tests.py
def test_03_post_form(self):
"""Test POSTing form data.
"""
data = {"foo": "bar", "baz": "qux"}
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert parameters["foo"] == "bar"
assert parameters["baz"] == "qux"
3
Example 4
Project: flask-exceptional Source File: tests.py
def test_04_post_file(self):
"""Test POSTing file data.
"""
resource = self.app.open_resource("README")
data = {"file": resource}
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "file" in parameters
3
Example 5
Project: flask-exceptional Source File: tests.py
def test_05_filter_header(self):
"""Test header data filtering.
"""
self.app.config["EXCEPTIONAL_HEADER_FILTER"] = ["Host"]
Exceptional(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
request = data["request"]
headers = request["headers"]
assert headers["Host"] == "[FILTERED]"
3
Example 6
Project: flask-exceptional Source File: tests.py
def test_06_filter_parameter(self):
"""Test parameter data filtering.
"""
data = {"foo": "bar", "baz": "qux"}
self.app.config["EXCEPTIONAL_PARAMETER_FILTER"] = ["baz"]
Exceptional(self.app)
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert parameters["baz"] == "[FILTERED]"
3
Example 7
Project: flask-exceptional Source File: tests.py
def test_09_debug(self):
"""Test exception in debug mode.
"""
self.app = self.create_application()
self.app.debug = True
exceptional = Exceptional(self.app)
self.app.config["EXCEPTIONAL_ENVIRONMENT_FILTER"].append("os.*")
self.app.config["PROPAGATE_EXCEPTIONS"] = None
assert exceptional.url == self.app.config["EXCEPTIONAL_DEBUG_URL"]
with self.app.test_client() as client:
self.assertRaises(ZeroDivisionError, client.get, "/error")
json.loads(g.exceptional)
print "See %s for HTTP request details." % exceptional.url
3
Example 8
Project: flask-exceptional Source File: tests.py
def test_11_utf8_decode(self):
"""Test sending an invalid UTF-8 byte sequence through Exceptional.
"""
self.app.config["INVALID_UTF-8"] = "\xf0"
Exceptional(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
application_environment = data["application_environment"]
environment = application_environment["env"]
assert environment["INVALID_UTF-8"] == u"\ufffd"
3
Example 9
Project: flask-exceptional Source File: tests.py
def test_12_json(self):
"""Test JSON request handling.
"""
self.app = self.create_application()
Exceptional(self.app)
data = json.dumps({"foo": {"bar": "baz"}})
with self.app.test_client() as client:
client.post("/error", content_type="application/json", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "bar" in parameters["foo"]
3
Example 10
Project: flask-exceptional Source File: tests.py
def test_13_invalid_json(self):
"""Test invalid JSON request handling.
"""
data = '{"foo": {"bar": invalid}}'
with self.app.test_client() as client:
client.post("/error", content_type="application/json", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "INVALID_JSON" in parameters
0
Example 11
Project: flask-exceptional Source File: flask_exceptional.py
@staticmethod
def test(config):
"""Test the given Flask configuration. If configured correctly,
an error will be tracked by Exceptional for your app. Unlike
the initialized extension, this test will post data to Exceptional,
regardless of the configured ``DEBUG`` setting.
:param config: The Flask application configuration object to test.
Accepts either :class:`flask.Config` or the object
types allowed by :meth:`flask.Config.from_object`.
"""
context = getattr(stack.top, "exceptional_context", None)
app = Flask(__name__)
exceptional = Exceptional()
if isinstance(config, Config):
app.config = config
else:
app.config.from_object(config)
assert "EXCEPTIONAL_API_KEY" in app.config
app.debug = False
app.testing = False
exceptional.init_app(app)
app.testing = True
@app.route("/exception")
def exception():
setattr(stack.top, "exceptional_context", context)
message = "Congratulations! Your application is configured for Exceptional error tracking." # NOQA
raise Exception(message)
with app.test_client() as client:
client.get("/exception")
json.loads(g.exceptional)
0
Example 12
def _post_data(self, context, traceback=None):
"""POST data to the the Exceptional API. If DEBUG is True then data is
sent to ``EXCEPTIONAL_DEBUG_URL`` if it has been defined. If TESTING is
true, error data is stored in the global ``flask.g.exceptional``
variable.
:param context: The current application or application context.
:param traceback: Default ``None``. The exception stack trace.
"""
if context:
if isinstance(context, Flask):
app = context
context = None
else:
app = context.app
else:
app = stack.top.app
application_data = self.__get_application_data(app)
client_data = {
"name": "flask-exceptional",
"version": self.__version__,
"protocol_version": self.__protocol_version
}
if context:
request_data = self.__get_request_data(app, context.request,
context.session)
context_data = getattr(context, "exceptional_context", None)
else:
request_data = None
context_data = None
traceback = traceback or tbtools.get_current_traceback()
exception_data = self.__get_exception_data(traceback)
encode_basestring = json.encoder.encode_basestring
def _encode_basestring(value):
if isinstance(value, str) and \
json.encoder.HAS_UTF8.search(value) is not None:
value = value.decode("utf-8",
"replace") # ensure the decode succeeds.
replace = lambda match: json.encoder.ESCAPE_DCT[match.group(0)]
return u'"%s"' % json.encoder.ESCAPE.sub(replace, value)
try:
json.encoder.encode_basestring = _encode_basestring
ret_val = json.dumps({
"application_environment": application_data,
"client": client_data,
"request": request_data,
"exception": exception_data,
"context": context_data
}, ensure_ascii=False).encode("utf-8")
finally:
json.encoder.encode_basestring = encode_basestring
if context and app.testing:
g.exceptional = ret_val
if self.url:
request = Request(self.url)
request.add_header("Content-Type", "application/json")
if app.debug:
data = ret_val
else:
request.add_header("Content-Encoding", "deflate")
data = compress(ret_val, 1)
try:
try:
urlopen(request, data)
except HTTPError, e:
if e.code >= 400:
raise
except URLError:
message = "Unable to connect to %s. See http://status.exceptional.io for details. Error data:\n%s" # NOQA
app.logger.warning(message, self.url, ret_val,
exc_info=True)
except BadStatusLine:
pass
return ret_val
0
Example 13
Project: flask-exceptional Source File: tests.py
def test_14_context(self):
"""Test exception context data.
"""
def exception_handler(app):
handle_exception = app.handle_exception
@wraps(handle_exception)
def ret_val(exception):
Exceptional.context(foo="bar")
return handle_exception(exception)
return ret_val
handle_exception = self.app.handle_exception
self.app.handle_exception = exception_handler(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
context = data["context"]
assert context["foo"] == "bar"
self.app.handle_exception = handle_exception
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
context = data["context"]
assert context is None