Browse Source

HUE-3957 [editor] Clicking on failed query should still open it and not another one

Romain Rigaux 9 năm trước cách đây
mục cha
commit
93f42db5f2

+ 32 - 21
desktop/libs/notebook/src/notebook/api.py

@@ -28,7 +28,7 @@ from django.views.decorators.http import require_GET, require_POST
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.django_util import JsonResponse
 from desktop.models import Document2, Document
 from desktop.models import Document2, Document
 
 
-from notebook.connectors.base import get_api, Notebook, QueryExpired, SessionExpired
+from notebook.connectors.base import get_api, Notebook, QueryExpired, SessionExpired, QueryError
 from notebook.decorators import api_error_handler, check_document_access_permission, check_document_modify_permission
 from notebook.decorators import api_error_handler, check_document_access_permission, check_document_modify_permission
 from notebook.github import GithubClient
 from notebook.github import GithubClient
 from notebook.models import escape_rows
 from notebook.models import escape_rows
@@ -107,26 +107,37 @@ def execute(request):
   snippet = json.loads(request.POST.get('snippet', '{}'))
   snippet = json.loads(request.POST.get('snippet', '{}'))
 
 
   try:
   try:
-    response['handle'] = get_api(request, snippet).execute(notebook, snippet)
-
-    # Retrieve and remove the result from the handle
-    if response['handle'].get('sync'):
-      result = response['handle'].pop('result')
-  finally:
-    if notebook['type'].startswith('query-'):
-      _snippet = [s for s in notebook['snippets'] if s['id'] == snippet['id']][0]
-      if 'handle' in response: # No failure
-        _snippet['result']['handle'] = response['handle']
-        _snippet['result']['statements_count'] = response['handle'].get('statements_count', 1)
-        _snippet['result']['statement_id'] = response['handle'].get('statement_id', 0)
-        _snippet['result']['handle']['statement'] = response['handle'].get('statement', snippet['statement']) # For non HS2, as non multi query yet
-      else:
-        _snippet['status'] = 'failed'
-      history = _historify(notebook, request.user)
-      response['history_id'] = history.id
-      response['history_uuid'] = history.uuid
-      if notebook['isSaved']: # Keep track of history of saved queries
-        response['history_parent_uuid'] = history.dependencies.filter(type__startswith='query-').latest('last_modified').uuid
+    try:
+      response['handle'] = get_api(request, snippet).execute(notebook, snippet)
+
+      # Retrieve and remove the result from the handle
+      if response['handle'].get('sync'):
+        result = response['handle'].pop('result')
+    finally:
+      if notebook['type'].startswith('query-'):
+        _snippet = [s for s in notebook['snippets'] if s['id'] == snippet['id']][0]
+        if 'handle' in response: # No failure
+          _snippet['result']['handle'] = response['handle']
+          _snippet['result']['statements_count'] = response['handle'].get('statements_count', 1)
+          _snippet['result']['statement_id'] = response['handle'].get('statement_id', 0)
+          _snippet['result']['handle']['statement'] = response['handle'].get('statement', snippet['statement']) # For non HS2, as non multi query yet
+        else:
+          _snippet['status'] = 'failed'
+
+        history = _historify(notebook, request.user)
+
+        response['history_id'] = history.id
+        response['history_uuid'] = history.uuid
+        if notebook['isSaved']: # Keep track of history of saved queries
+          response['history_parent_uuid'] = history.dependencies.filter(type__startswith='query-').latest('last_modified').uuid
+  except QueryError, ex: # We inject the history information from _historify() to the failed queries
+    if response.get('history_id'):
+      ex.extra['history_id'] = response['history_id']
+    if response.get('history_uuid'):
+      ex.extra['history_uuid'] = response['history_uuid']
+    if response.get('history_parent_uuid'):
+      ex.extra['history_parent_uuid'] = response['history_parent_uuid']
+    raise ex
 
 
   # Inject and HTML escape results
   # Inject and HTML escape results
   if result is not None:
   if result is not None:

+ 1 - 0
desktop/libs/notebook/src/notebook/connectors/base.py

@@ -42,6 +42,7 @@ class QueryError(Exception):
   def __init__(self, message, handle=None):
   def __init__(self, message, handle=None):
     self.message = message
     self.message = message
     self.handle = handle
     self.handle = handle
+    self.extra = {}
 
 
   def __str__(self):
   def __str__(self):
     return force_unicode(str(self.message))
     return force_unicode(str(self.message))

+ 2 - 0
desktop/libs/notebook/src/notebook/decorators.py

@@ -95,6 +95,8 @@ def api_error_handler(func):
       response['message'] = force_unicode(str(e))
       response['message'] = force_unicode(str(e))
       if e.handle:
       if e.handle:
         response['handle'] = e.handle
         response['handle'] = e.handle
+      if e.extra:
+        response.update(e.extra)
     except Exception, e:
     except Exception, e:
       LOG.exception('Error running %s' % func)
       LOG.exception('Error running %s' % func)
       response['status'] = -1
       response['status'] = -1

+ 1 - 0
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -628,6 +628,7 @@
         snippet: ko.mapping.toJSON(self.getContext())
         snippet: ko.mapping.toJSON(self.getContext())
       }, function (data) {
       }, function (data) {
         self.statusForButtons('executed');
         self.statusForButtons('executed');
+
         if (vm.editorMode && data.history_id) {
         if (vm.editorMode && data.history_id) {
           var url = '/notebook/editor?editor=' + data.history_id;
           var url = '/notebook/editor?editor=' + data.history_id;
           hueUtils.changeURL(url);
           hueUtils.changeURL(url);