Refactor and simplify query tool connection error handling code. Fixes #3235

This commit is contained in:
Murtuza Zabuawala
2018-04-04 11:20:36 +01:00
committed by Dave Page
parent be055ce57d
commit a705fb46a8
8 changed files with 393 additions and 607 deletions

View File

@@ -11,7 +11,7 @@ import gettext from '../gettext';
import $ from 'jquery';
import url_for from '../url_for';
import axios from 'axios';
import * as transaction from './is_new_transaction_required';
import * as httpErrorHandler from './query_tool_http_error_handler';
class LoadingScreen {
constructor(sqlEditor) {
@@ -152,8 +152,8 @@ class ExecuteQuery {
const errorData = error.response.data;
if (self.userManagement.is_pga_login_required(errorData)) {
return self.userManagement.pga_login();
if (self.userManagement.isPgaLoginRequired(errorData)) {
return self.userManagement.pgaLogin();
}
let msg = ExecuteQuery.extractErrorMessage(errorData);
@@ -198,18 +198,18 @@ class ExecuteQuery {
return;
}
if (this.userManagement.is_pga_login_required(httpMessage.response)) {
this.sqlServerObject.save_state('execute', [this.explainPlan]);
this.userManagement.pga_login();
if (this.userManagement.isPgaLoginRequired(httpMessage.response)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]);
this.userManagement.pgaLogin();
}
if (transaction.is_new_transaction_required(httpMessage.response)) {
this.sqlServerObject.save_state('execute', [this.explainPlan]);
this.sqlServerObject.init_transaction();
if (httpErrorHandler.httpResponseRequiresNewTransaction(httpMessage.response)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]);
this.sqlServerObject.initTransaction();
}
if (this.wasDatabaseConnectionLost(httpMessage)) {
this.sqlServerObject.save_state('execute', [this.explainPlan]);
this.sqlServerObject.saveState('execute', [this.explainPlan]);
this.sqlServerObject.handle_connection_lost(false, httpMessage);
}

View File

@@ -1,23 +0,0 @@
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2018, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
export function is_new_transaction_required(xhr) {
/* If responseJSON is undefined then it could be object of
* axios(Promise HTTP) response, so we should check accordingly.
*/
if (xhr.responseJSON === undefined && xhr.data !== undefined) {
return xhr.status === 404 && xhr.data &&
xhr.data.info &&
xhr.data.info === 'DATAGRID_TRANSACTION_REQUIRED';
}
return xhr.status === 404 && xhr.responseJSON &&
xhr.responseJSON.info &&
xhr.responseJSON.info === 'DATAGRID_TRANSACTION_REQUIRED';
}

View File

@@ -0,0 +1,76 @@
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2018, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
import gettext from 'sources/gettext';
export function httpResponseRequiresNewTransaction(xhr) {
/* If responseJSON is undefined then it could be object of
* axios(Promise HTTP) response, so we should check accordingly.
*/
if (xhr.responseJSON === undefined && xhr.data !== undefined) {
return xhr.status === 404 && xhr.data &&
xhr.data.info &&
xhr.data.info === 'DATAGRID_TRANSACTION_REQUIRED';
}
return xhr.status === 404 && xhr.responseJSON &&
xhr.responseJSON.info &&
xhr.responseJSON.info === 'DATAGRID_TRANSACTION_REQUIRED';
}
// Allow us to redirect to login dialog and if required then re-initiate the transaction
export function handleLoginRequiredAndTransactionRequired(
pgAdmin, handler, exception, stateToSave, stateParameters, checkTransaction
) {
stateParameters = stateParameters && stateParameters.length > 0 ? stateParameters : [];
if (pgAdmin.Browser.UserManagement.isPgaLoginRequired(exception)) {
if (stateToSave) {
handler.saveState(stateToSave, stateParameters);
}
return pgAdmin.Browser.UserManagement.pgaLogin();
}
if(checkTransaction && httpResponseRequiresNewTransaction(exception)) {
if (stateToSave) {
handler.saveState(stateToSave, stateParameters);
}
return handler.initTransaction();
}
}
// Allow us to handle the AJAX error from Query tool
export function handleQueryToolAjaxError(
pgAdmin, handler, exception, stateToSave, stateParameters, checkTransaction
) {
if (exception.readyState === 0) {
return gettext('Not connected to the server or the connection to the server has been closed.');
}
handleLoginRequiredAndTransactionRequired(
pgAdmin, handler, exception, stateToSave, stateParameters, checkTransaction
);
let msg = exception.responseText;
if (exception.responseJSON !== undefined) {
if(exception.responseJSON.errormsg !== undefined) {
msg = exception.responseJSON.errormsg;
}
if(exception.status === 503 && exception.responseJSON.info !== undefined &&
exception.responseJSON.info == 'CONNECTION_LOST') {
setTimeout(function() {
if (stateToSave) {
handler.saveState(stateToSave, stateParameters);
}
handler.handle_connection_lost(false, exception);
});
}
}
return msg;
}