jenkins-bot has submitted this change and it was merged.

Change subject: Refactor error extraction and move to api
......................................................................


Refactor error extraction and move to api

Bug: T147114
Bug: T144666
Change-Id: I0c58cdcee72b25c60ec02b68bf25f4029641af01
---
M i18n/en.json
M i18n/qqq.json
M wikibase/queryService/api/Sparql.js
M wikibase/queryService/ui/App.js
4 files changed, 89 insertions(+), 48 deletions(-)

Approvals:
  Smalyshev: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/i18n/en.json b/i18n/en.json
index c9d68ba..078a21d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -48,5 +48,7 @@
     "wdqs-action-error-display": "Unable to display result",
     "wdqs-action-timeout": "Query timeout limit reached",
     "wdqs-action-malformed-query": "Query is malformed",
+    "wdqs-action-server-error": "Server error",
+    "wdqs-action-unknow-error": "Unknown error",
     "wdqs-result-map-layers-all": "All layers"
 }
\ No newline at end of file
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 6eeaf23..31299e7 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -50,5 +50,7 @@
        "wdqs-action-error-display": "Label for progressbar",
        "wdqs-action-timeout": "Label for progressbar",
        "wdqs-action-malformed-query": "Label for progressbar",
+    "wdqs-action-server-error": "Label for server error",
+       "wdqs-action-unknow-error": "Label for unknown error",
        "wdqs-result-map-layers-all": "Label for all layers in layer control on 
map view"
 }
diff --git a/wikibase/queryService/api/Sparql.js 
b/wikibase/queryService/api/Sparql.js
index 4d688d6..98bf89f 100644
--- a/wikibase/queryService/api/Sparql.js
+++ b/wikibase/queryService/api/Sparql.js
@@ -7,6 +7,18 @@
 
        var SPARQL_SERVICE_URI = 
'//query.wikidata.org/bigdata/namespace/wdq/sparql';
 
+       var ERROR_CODES = {
+                       TIMEOUT: 10,
+                       MALFORMED: 20,
+                       SERVER: 30,
+                       UNKNOWN: 100
+       };
+
+       var ERROR_MAP = {
+               'QueryTimeoutException: Query deadline is expired': 
ERROR_CODES.TIMEOUT,
+               'MalformedQueryException: ': ERROR_CODES.MALFORMED
+       };
+
        /**
         * SPARQL API for the Wikibase query service
         *
@@ -24,6 +36,11 @@
        }
 
        /**
+        * @property {Object}
+        */
+       SELF.prototype.ERROR_CODES = ERROR_CODES;
+
+       /**
         * @property {Number}
         * @private
         */
@@ -36,10 +53,10 @@
        SELF.prototype._executionTime = null;
 
        /**
-        * @property {string}
+        * @property {Object}
         * @private
         */
-       SELF.prototype._errorMessage = null;
+       SELF.prototype._error = null;
 
        /**
         * @property {Number}
@@ -109,9 +126,7 @@
         * @return {jQuery.Promise} query
         */
        SELF.prototype.query = function( query ) {
-               var self = this,
-                       deferred = $.Deferred(),
-                       settings = {
+               var self = this, deferred = $.Deferred(), settings = {
                        headers: {
                                Accept: 'application/sparql-results+json'
                        }
@@ -120,7 +135,7 @@
                this._queryUri = this._serviceUri + '?query=' + 
encodeURIComponent( query );
 
                this._executionTime = Date.now();
-               $.ajax( this._queryUri, settings ).done( function( data, 
textStatus, jqXHR ) {
+               $.ajax( this._queryUri, settings ).done( function( data, 
textStatus, request ) {
                        self._executionTime = Date.now() - self._executionTime;
 
                        if ( typeof data.boolean === 'boolean' ) {
@@ -131,11 +146,11 @@
                        self._rawData = data;
 
                        deferred.resolve();
-               } ).fail( function( jqXHR ) {
+               } ).fail( function( request ) {
                        self._executionTime = null;
                        self._rawData = null;
                        self._resultLength = null;
-                       self._generateErrorMessage( jqXHR );
+                       self._generateErrorMessage( request );
 
                        deferred.reject();
                } );
@@ -146,18 +161,41 @@
        /**
         * Get execution time in ms of the submitted query
         */
-       SELF.prototype._generateErrorMessage = function( jqXHR ) {
-               var message = 'ERROR: ';
+       SELF.prototype._generateErrorMessage = function( request, options, 
exception ) {
+               var error = {
+                       code: null,
+                       message: null,
+                       debug: request.responseText
+               };
 
-               if ( jqXHR.status === 0 ) {
-                       message += 'Could not contact server';
+               if ( request.status === 0 ) {
+                       error.code = ERROR_CODES.SERVER;
+                       error.message = exception;
                } else {
-                       message += jqXHR.responseText;
-                       if ( jqXHR.responseText.match( /Query deadline is 
expired/ ) ) {
-                               message = 'QUERY TIMEOUT\n' + message;
+
+                       try {
+                               var errorToMatch = error.debug.substring( 
error.debug
+                                               .indexOf( 
'java.util.concurrent.ExecutionException:' ) );
+
+                               for ( var errorKey in ERROR_MAP ) {
+                                       if ( errorToMatch.indexOf( errorKey ) 
!== -1 ) {
+                                               error.code = ERROR_MAP[ 
errorKey ];
+                                       }
+                               }
+
+                               if ( error.code === null || error.code === 
ERROR_CODES.MALFORMED ) {
+                                       error.message = error.debug
+                                                       .match(
+                                                                       
/(java\.util\.concurrent\.ExecutionException\:)+(.*)(Exception\:)+(.*)/ )
+                                                       .pop().trim();
+                               }
+
+                       } catch ( e ) {
+                               error.code = ERROR_CODES.UNKNOWN;
                        }
                }
-               this._errorMessage = message;
+
+               this._error = error;
        };
 
        /**
@@ -170,12 +208,12 @@
        };
 
        /**
-        * Get error message of the submitted query if it has failed
+        * Get error of the submitted query if it has failed
         *
-        * @return {Number}
+        * @return {object}
         */
-       SELF.prototype.getErrorMessage = function() {
-               return this._errorMessage;
+       SELF.prototype.getError = function() {
+               return this._error;
        };
 
        /**
diff --git a/wikibase/queryService/ui/App.js b/wikibase/queryService/ui/App.js
index 4c0a524..25e6edf 100644
--- a/wikibase/queryService/ui/App.js
+++ b/wikibase/queryService/ui/App.js
@@ -10,11 +10,6 @@
 
        var TRACKING_NAMESPACE = 'wikibase.queryService.ui.app.';
 
-       var QUERY_ERROR_MAP = {
-                       'QueryTimeoutException: Query deadline is expired': 
'wdqs-action-timeout',
-                       'MalformedQueryException: ': 
'wdqs-action-malformed-query'
-       };
-
        /**
         * A ui application for the Wikibase query service
         *
@@ -603,10 +598,9 @@
                $( '#execute-button' ).prop( 'disabled', true );
                $( '#query-error' ).hide();
 
-               this._sparqlApi.query( this._editor.getValue() ).done(
-                               $.proxy( this._handleQueryResult, this ) 
).fail( function() {
-                       self._handleQueryError( 
self._sparqlApi.getErrorMessage() );
-               } );
+               this._sparqlApi.query( this._editor.getValue() )
+                       .done( $.proxy( this._handleQueryResult, this ) )
+                       .fail( $.proxy( this._handleQueryError, this ) );
 
                $( '.queryUri' ).attr( 'href', self._sparqlApi.getQueryUri() );
        };
@@ -614,28 +608,33 @@
        /**
         * @private
         */
-       SELF.prototype._handleQueryError = function( error ) {
-               $( '#query-error' ).html( $( '<pre>' ).text( error ) ).show();
-               var shortError = null,
-                       errorMessageKey = null,
-                       errorToMatch = error.substring( error.indexOf( 
'java.util.concurrent.ExecutionException:' ) );
-
-               for ( var errorKey in QUERY_ERROR_MAP ) {
-                       if ( errorToMatch.indexOf( errorKey ) !== -1 ) {
-                               errorMessageKey = QUERY_ERROR_MAP[ errorKey ];
-                       }
-               }
-               if ( errorMessageKey === null || errorMessageKey === 
'wdqs-action-malformed-query' ) {
-                       shortError = error.match(
-                               
/(java\.util\.concurrent\.ExecutionException\:)+(.*)(Exception\:)+(.*)/ 
).pop().trim();
-               }
-
-               this._actionBar.show( errorMessageKey || '', shortError || '', 
'danger' );
+       SELF.prototype._handleQueryError = function() {
                $( '#execute-button' ).prop( 'disabled', false );
 
-               this._track( 'result.error.' + ( QUERY_ERROR_MAP[ shortError ] 
|| 'unknown' ) );
+               var error = this._sparqlApi.getError(), errorMessageKey = null, 
codes = this._sparqlApi.ERROR_CODES;
 
-               this._editor.highlightError( error );
+               switch ( error.code ) {
+               case codes.TIMEOUT:
+                       errorMessageKey = 'wdqs-action-timeout';
+                       break;
+               case codes.MALFORMED:
+                       errorMessageKey = 'wdqs-action-malformed-query';
+                       break;
+               case codes.SERVER:
+                       errorMessageKey = 'wdqs-action-server-error';
+                       break;
+               default:
+                       errorMessageKey = 'wdqs-action-unknow-error';
+                       break;
+               }
+
+               if ( error.debug ) {
+                       $( '#query-error' ).html( $( '<pre>' ).text( 
error.debug ) ).show();
+               }
+
+               this._actionBar.show( errorMessageKey || '', error.message || 
'', 'danger' );
+               this._track( 'result.error.' + ( errorMessageKey || 'unknown' ) 
);
+               this._editor.highlightError( error.debug );
        };
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/315505
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0c58cdcee72b25c60ec02b68bf25f4029641af01
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to