Handle connection errors in UI

pull/15459/head
alejandraarri 5 years ago
parent e8cf309179
commit d655adaa46

@ -74,6 +74,22 @@ $w: $sLayout-width;
padding-top: 12px; padding-top: 12px;
} }
&__form-error {
position: relative;
width: 465px;
padding-left: 20px;
color: $cHighlight-negative;
font-size: 12px;
line-height: 16px;
&::before {
content: '🚫';
position: absolute;
left: 0;
line-height: 18px;
}
}
&__input-error { &__input-error {
margin-top: -4px; margin-top: -4px;
padding: 0 12px; padding: 0 12px;

@ -25,7 +25,10 @@ module.exports = CoreView.extend({
}, },
render: function () { render: function () {
this.$el.html(template(this.options)); this.$el.html(template({
title: this.options.title,
errorMessage: this.model.get('errorMessage')
}));
this._addSidebar(); this._addSidebar();
this.form = { this.form = {
@ -37,6 +40,13 @@ module.exports = CoreView.extend({
password: this.$('.js-password') password: this.$('.js-password')
}; };
if (this._model.connection) {
this.form.server.val(this._model.connection.server);
this.form.port.val(this._model.connection.port);
this.form.database.val(this._model.connection.database);
this.form.username.val(this._model.connection.username);
this.form.password.val(this._model.connection.password);
}
return this; return this;
}, },
@ -52,7 +62,7 @@ module.exports = CoreView.extend({
_checkVisibility: function () { _checkVisibility: function () {
const state = this._model.get('state'); const state = this._model.get('state');
if (state === 'idle' || state === 'error') { if (state === 'idle') {
this.show(); this.show();
} else { } else {
this.hide(); this.hide();
@ -96,22 +106,24 @@ module.exports = CoreView.extend({
client.checkDBConnectorsConnection(this._service, params, (errors, _response, data) => { client.checkDBConnectorsConnection(this._service, params, (errors, _response, data) => {
if (errors) { if (errors) {
this._checkConnectionError(); this._checkConnectionError(data);
} else if (data && data.connected) {
this._checkConnectionSuccess();
} }
this._checkConnectionSuccess(data);
}); });
}, },
_checkConnectionSuccess: function (data) { _checkConnectionSuccess: function () {
if (data && data.connected) { this._model.set('state', 'connected');
this._model.set('state', 'connected'); this._model.set('service_name', 'connector');
this._model.set('service_name', 'connector');
} else {
this._model.set('state', 'error');
}
}, },
_checkConnectionError: function () { _checkConnectionError: function (data) {
this._model.set('state', 'error'); if (data.responseJSON && data.responseJSON.connected === false) {
this._model.set('errorMessage', _t('components.modals.add-layer.imports.database.connection-error'));
} else {
this._model.set('errorMessage', _t('components.modals.add-layer.imports.database.general-error'));
}
this.render();
} }
}); });

@ -50,6 +50,14 @@
</div> </div>
</div> </div>
<% if (errorMessage) { %>
<div class="Form-row">
<div class="ImportOptions__form-error CDB-Text">
<%- errorMessage %>
</div>
</div>
<% } %>
<div class="Form-row"> <div class="Form-row">
<div class="Form-rowLabel Form-rowLabel--small"></div> <div class="Form-rowLabel Form-rowLabel--small"></div>
<div class="Form-rowData u-flex__justify--end"> <div class="Form-rowData u-flex__justify--end">

@ -20,6 +20,10 @@ module.exports = ImportDataView.extend({
); );
this._initCodeMirror(); this._initCodeMirror();
if (this.sqlQuery) {
this.codeEditor.setValue(this.sqlQuery);
}
return this; return this;
}, },
@ -55,6 +59,8 @@ module.exports = ImportDataView.extend({
return; return;
} }
this.sqlQuery = query;
this.trigger('urlSelected', this); this.trigger('urlSelected', this);
// Change model // Change model
@ -95,7 +101,7 @@ module.exports = ImportDataView.extend({
this.model.set('service_item_id', JSON.stringify(connector)); this.model.set('service_item_id', JSON.stringify(connector));
} catch (e) { } catch (e) {
this.model.set('errorMessage', _t('components.modals.add-layer.imports.database.sql-error')); this.model.set('errorMessage', _t('components.modals.add-layer.imports.database.sql-error'));
this.model.set('state', 'list'); this.model.set('state', 'connected');
this.model.setUpload({ this.model.setUpload({
value: '', value: '',
service_item_id: '' service_item_id: ''

@ -1373,6 +1373,8 @@
"label-password": "Password", "label-password": "Password",
"placeholder-password": "Type your password", "placeholder-password": "Type your password",
"connect-button": "Connect", "connect-button": "Connect",
"connection-error": "The connection could not be established. Please, check the entered parameters.",
"general-error": "Unfortunately, there was an error. Please, try again.",
"field-sql-query": "SQL query", "field-sql-query": "SQL query",
"sql-hint": "If your query contains geographic data in EPSG:4326 format, name that column with an accepted name so that its imported correctly: “the_geom”, “geom“, “geometry“...", "sql-hint": "If your query contains geographic data in EPSG:4326 format, name that column with an accepted name so that its imported correctly: “the_geom”, “geom“, “geometry“...",
"sql-error": "There has been an error parsing your query", "sql-error": "There has been an error parsing your query",

@ -99,26 +99,21 @@ describe('components/modals/add-layer/content/imports/import-database/import-dat
}); });
describe('_checkConnectionSuccess', function () { describe('_checkConnectionSuccess', function () {
it('should set model state `connected` if Import API call succeeds', function () { it('should set model state `connected`', function () {
this.view._checkConnectionSuccess({ status: 200, connected: true }); this.view._checkConnectionSuccess();
expect(this.view.model.get('state')).toEqual('connected'); expect(this.view.model.get('state')).toEqual('connected');
}); });
it('should set model service_name `connector` if Import API call succeeds', function () {
this.view._checkConnectionSuccess({ status: 200, connected: true });
expect(this.view.model.get('state')).toEqual('connected');
});
it('should set model state `error` if Import API call succeeds but does not connect', function () {
this.view._checkConnectionSuccess({ status: 400, connected: false });
expect(this.view.model.get('state')).toEqual('error');
});
}); });
describe('_checkConnectionError', function () { describe('_checkConnectionError', function () {
it('should set model state `error` if Import API call does not succeed', function () { it('should set an error message if Import API call returns errors and `connected` is false', function () {
this.view._checkConnectionSuccess({ status: 400, connected: false }); this.view._checkConnectionError({ status: 400, responseJSON: { connected: false } });
expect(this.view.model.get('state')).toEqual('error'); expect(this.view.model.get('errorMessage')).toBe('components.modals.add-layer.imports.database.connection-error');
});
it('should set a general error message if Import API call returns errors', function () {
this.view._checkConnectionError({ status: 400 });
expect(this.view.model.get('errorMessage')).toBe('components.modals.add-layer.imports.database.general-error');
}); });
}); });
}); });

Loading…
Cancel
Save