From 2e5dfd7974e80182c0bdbc2e24a5e13cd566855b Mon Sep 17 00:00:00 2001 From: Waldecir Loureiro dos Santos Filho Date: Mon, 19 Mar 2012 09:34:12 -0300 Subject: [PATCH] Return JSON Error message from facebook, this is useful to some functional logics, ex,: in some cases like missing some keys in dict like actions, actions need ("name", "link") keys, if you miss facebook return the error message. --- tornado/auth.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tornado/auth.py b/tornado/auth.py index 21c8ac8803..ab29d1bec1 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -1000,7 +1000,21 @@ def _on_access_token(self, redirect_uri, client_id, client_secret, callback, fields, response): if response.error: logging.warning('Facebook auth error: %s' % str(response)) - callback(None) + + try: + json = escape.json_decode(response.body) + except Exception: + logging.warning("Invalid JSON from Facebook: %r", response.body) + callback(None) + return + + if isinstance(json, dict) and json.get("error_code"): + logging.warning("Facebook error: %d: %r", json["error_code"], + json.get("error_msg")) + callback(None) + return + + callback(json) return args = escape.parse_qs_bytes(escape.native_str(response.body)) @@ -1084,8 +1098,24 @@ def _on_facebook_request(self, callback, response): if response.error: logging.warning("Error response %s fetching %s", response.error, response.request.url) - callback(None) + + try: + json = escape.json_decode(response.body) + except Exception: + logging.warning("Invalid JSON from Facebook: %r", response.body) + callback(None) + return + + if isinstance(json, dict) and json.get("error_code"): + logging.warning("Facebook error: %d: %r", json["error_code"], + json.get("error_msg")) + callback(None) + return + + callback(json) return + + callback(escape.json_decode(response.body))