Просмотр исходного кода

HUE-4043 [editor] Bubble up error about queries with \u2002 fails

Romain Rigaux 9 лет назад
Родитель
Сommit
03a620f

+ 3 - 3
desktop/libs/notebook/src/notebook/connectors/base.py

@@ -21,7 +21,7 @@ import logging
 from django.utils.translation import ugettext as _
 
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.i18n import force_unicode
+from desktop.lib.i18n import smart_unicode
 
 from notebook.conf import get_interpreters
 
@@ -44,8 +44,8 @@ class QueryError(Exception):
     self.handle = handle
     self.extra = {}
 
-  def __str__(self):
-    return force_unicode(str(self.message))
+  def __unicode__(self):
+    return smart_unicode(self.message)
 
 
 class Notebook(object):

+ 3 - 3
desktop/libs/notebook/src/notebook/decorators.py

@@ -25,7 +25,7 @@ from django.utils.translation import ugettext as _
 
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.i18n import force_unicode
+from desktop.lib.i18n import smart_unicode
 from desktop.models import Document2, Document
 
 from notebook.connectors.base import QueryExpired, QueryError, SessionExpired, AuthenticationRequired
@@ -92,7 +92,7 @@ def api_error_handler(func):
     except QueryError, e:
       LOG.exception('Error running %s' % func)
       response['status'] = 1
-      response['message'] = force_unicode(str(e))
+      response['message'] = smart_unicode(e)
       if e.handle:
         response['handle'] = e.handle
       if e.extra:
@@ -100,7 +100,7 @@ def api_error_handler(func):
     except Exception, e:
       LOG.exception('Error running %s' % func)
       response['status'] = -1
-      response['message'] = force_unicode(str(e))
+      response['message'] = smart_unicode(e)
     finally:
       if response:
         return JsonResponse(response)

+ 33 - 0
desktop/libs/notebook/src/notebook/tests.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+## -*- coding: utf-8 -*-
 # Licensed to Cloudera, Inc. under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -27,6 +28,8 @@ from desktop.lib.test_utils import grant_access
 from desktop.models import Directory, Document, Document2
 
 from notebook.api import _historify
+from notebook.connectors.base import QueryError
+from notebook.decorators import api_error_handler
 
 
 class TestNotebookApi(object):
@@ -173,3 +176,33 @@ class TestNotebookApi(object):
     assert_false(Document2.objects.filter(type='query-hive', is_history=True).exists())
     assert_true(Document2.objects.filter(type='query-hive', is_history=False).exists())
     assert_true(Document2.objects.filter(type='query-impala', is_history=True).exists())
+
+
+  def test_query_error_encoding(self):
+    @api_error_handler
+    def send_exception(message):
+      raise QueryError(message=message)
+
+    message = """SELECT
+a.key,
+a.*
+FROM customers c, c.addresses a"""
+    response =send_exception(message)
+    data = json.loads(response.content)
+    assert_equal(1, data['status'])
+
+    message = """SELECT
+\u2002\u2002a.key,
+\u2002\u2002a.*
+FROM customers c, c.addresses a"""
+    response =send_exception(message)
+    data = json.loads(response.content)
+    assert_equal(1, data['status'])
+
+    message = u"""SELECT
+a.key,
+a.*
+FROM déclenché c, c.addresses a"""
+    response =send_exception(message)
+    data = json.loads(response.content)
+    assert_equal(1, data['status'])