mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Code improvements to handle API errors gracefully in tree code. #6308
This commit is contained in:
@@ -543,6 +543,9 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
data: save_data,
|
data: save_data,
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
let requiresTreeRefresh = save_data.some((s)=>{
|
||||||
|
return s.name=='show_system_objects'||s.name=='show_empty_coll_nodes'||s.name.startsWith('show_node_')||s.name=='hide_shared_server';
|
||||||
|
});
|
||||||
let requires_refresh = false;
|
let requires_refresh = false;
|
||||||
/* Find the modules changed */
|
/* Find the modules changed */
|
||||||
let modulesChanged = {};
|
let modulesChanged = {};
|
||||||
@@ -559,25 +562,6 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
|
|
||||||
requires_refresh = checkRefreshRequired(pref, requires_refresh);
|
requires_refresh = checkRefreshRequired(pref, requires_refresh);
|
||||||
|
|
||||||
if (pref.name == 'hide_shared_server') {
|
|
||||||
Notify.confirm(
|
|
||||||
gettext('Object explorer tree refresh required'),
|
|
||||||
gettext('The object explorer tree refresh is required. Do you wish to refresh the tree?'),
|
|
||||||
function () {
|
|
||||||
pgAdmin.Browser.tree.destroy({
|
|
||||||
success: function () {
|
|
||||||
pgAdmin.Browser.initializeBrowserTree(pgAdmin.Browser);
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
gettext('Refresh'),
|
|
||||||
gettext('Later')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Sync the lock layout menu with preferences
|
// Sync the lock layout menu with preferences
|
||||||
if (pref.name == 'lock_layout') {
|
if (pref.name == 'lock_layout') {
|
||||||
let fileMenu = pgAdmin.Browser.MainMenus.find((menu) => menu.name == 'file');
|
let fileMenu = pgAdmin.Browser.MainMenus.find((menu) => menu.name == 'file');
|
||||||
@@ -593,6 +577,26 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (requiresTreeRefresh) {
|
||||||
|
Notify.confirm(
|
||||||
|
gettext('Object explorer tree refresh required'),
|
||||||
|
gettext('The object explorer tree refresh is required. Do you wish to refresh the tree?'),
|
||||||
|
function () {
|
||||||
|
pgAdmin.Browser.tree.destroy({
|
||||||
|
success: function () {
|
||||||
|
pgAdmin.Browser.initializeBrowserTree(pgAdmin.Browser);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function () {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
gettext('Refresh'),
|
||||||
|
gettext('Later')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (requires_refresh) {
|
if (requires_refresh) {
|
||||||
Notify.confirm(
|
Notify.confirm(
|
||||||
gettext('Refresh required'),
|
gettext('Refresh required'),
|
||||||
|
@@ -17,6 +17,7 @@ import Notify from '../../../static/js/helpers/Notifier';
|
|||||||
import gettext from 'sources/gettext';
|
import gettext from 'sources/gettext';
|
||||||
|
|
||||||
import { unix } from 'path-fx';
|
import { unix } from 'path-fx';
|
||||||
|
import getApiInstance, { parseApiError } from '../api_instance';
|
||||||
|
|
||||||
export class ManageTreeNodes {
|
export class ManageTreeNodes {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -73,94 +74,78 @@ export class ManageTreeNodes {
|
|||||||
res(treeNode);
|
res(treeNode);
|
||||||
});
|
});
|
||||||
|
|
||||||
public readNode = (_path: string) => new Promise<string[]>((res, rej) => {
|
public readNode = async (_path: string) => {
|
||||||
let temp_tree_path = _path;
|
let temp_tree_path = _path;
|
||||||
const node = this.findNode(_path);
|
const node = this.findNode(_path);
|
||||||
const base_url = pgAdmin.Browser.URL;
|
const base_url = pgAdmin.Browser.URL;
|
||||||
|
const api = getApiInstance();
|
||||||
|
|
||||||
if (node && node.children.length > 0) {
|
if (node && node.children.length > 0) {
|
||||||
if (!node.type === FileType.File) {
|
if (node.type !== FileType.File) {
|
||||||
rej('It\'s a leaf node');
|
console.error(node, 'It\'s a leaf node');
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (node.children.length != 0) res(node.children);
|
if (node.children.length != 0) return node.children;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
let url = '';
|
||||||
async function loadData() {
|
if (_path == '/browser') {
|
||||||
let url = '';
|
url = url_for('browser.nodes');
|
||||||
if (_path == '/browser') {
|
} else {
|
||||||
url = url_for('browser.nodes');
|
const _parent_url = self.generate_url(_path);
|
||||||
} else {
|
if (node.metadata.data._pid == null ) {
|
||||||
const _parent_url = self.generate_url(_path);
|
url = node.metadata.data._type + '/children/' + node.metadata.data._id;
|
||||||
if (node.metadata.data._pid == null ) {
|
}
|
||||||
url = node.metadata.data._type + '/children/' + node.metadata.data._id;
|
else {
|
||||||
|
if (node.metadata.data._type.includes('coll-')) {
|
||||||
|
const _type = node.metadata.data._type.replace('coll-', '');
|
||||||
|
url = _type + '/nodes/' + _parent_url + '/';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (node.metadata.data._type.includes('coll-')) {
|
url = node.metadata.data._type + '/children/' + _parent_url + '/' + node.metadata.data._id;
|
||||||
const _type = node.metadata.data._type.replace('coll-', '');
|
|
||||||
url = _type + '/nodes/' + _parent_url + '/';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
url = node.metadata.data._type + '/children/' + _parent_url + '/' + node.metadata.data._id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
url = base_url + url;
|
|
||||||
|
|
||||||
temp_tree_path = node.path;
|
|
||||||
|
|
||||||
if (node.metadata.data._type == 'server' && !node.metadata.data.connected) {
|
|
||||||
url = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function jsonData(fetch_url) {
|
url = base_url + url;
|
||||||
const result = await fetch(fetch_url, {
|
|
||||||
headers: {
|
|
||||||
'X-Requested-With': 'XMLHttpRequest',
|
|
||||||
'X-pgA-CSRFToken': pgAdmin.csrf_token
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.status == 200) {
|
temp_tree_path = node.path;
|
||||||
try {
|
|
||||||
const json = await result.json();
|
if (node.metadata.data._type == 'server' && !node.metadata.data.connected) {
|
||||||
return json.data;
|
url = null;
|
||||||
} catch (e) {
|
|
||||||
console.warn(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error('Node Load Error...');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let treeData = null;
|
|
||||||
if (url) treeData = await jsonData(url);
|
|
||||||
|
|
||||||
const Path = BrowserFS.BFSRequire('path');
|
|
||||||
const fill = async (tree) => {
|
|
||||||
for (const idx in tree) {
|
|
||||||
const _node = tree[idx];
|
|
||||||
const _pathl = Path.join(_path, _node.id);
|
|
||||||
await self.addNode(temp_tree_path, _pathl, _node);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await fill(treeData);
|
|
||||||
if (node.children.length > 0) res(node.children);
|
|
||||||
else {
|
|
||||||
res([]);
|
|
||||||
if (node.data && node.data._type == 'server' && node.data.connected) {
|
|
||||||
Notify.info(gettext('Server children are not available.'
|
|
||||||
+' Please check these nodes are not hidden through the preferences setting `Browser > Nodes`.'), null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
loadData();
|
|
||||||
});
|
let treeData = [];
|
||||||
|
if (url) {
|
||||||
|
try {
|
||||||
|
const res = await api.get(url);
|
||||||
|
treeData = res.data.data;
|
||||||
|
} catch (error) {
|
||||||
|
/* react-aspen does not handle reject case */
|
||||||
|
console.error(error);
|
||||||
|
Notify.error(parseApiError(error)||'Node Load Error...');
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Path = BrowserFS.BFSRequire('path');
|
||||||
|
for (const idx in treeData) {
|
||||||
|
const _node: any = treeData[idx];
|
||||||
|
const _pathl = Path.join(_path, _node.id);
|
||||||
|
await self.addNode(temp_tree_path, _pathl, _node);
|
||||||
|
}
|
||||||
|
if (node.children.length > 0) return node.children;
|
||||||
|
else {
|
||||||
|
if (node.data && node.data._type == 'server' && node.data.connected) {
|
||||||
|
Notify.info(gettext('Server children are not available.'
|
||||||
|
+' Please check these nodes are not hidden through the preferences setting `Browser > Nodes`.'), null);
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public generate_url = (path: string) => {
|
public generate_url = (path: string) => {
|
||||||
let _path = path;
|
let _path = path;
|
||||||
|
Reference in New Issue
Block a user