Skip to content

Commit 8efd063

Browse files
author
Christian Guy
committed
improve error handling if unable to reach icp4d master node
1 parent 901926b commit 8efd063

6 files changed

Lines changed: 68 additions & 36 deletions

File tree

lib/MessageHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default class MessageHandler {
125125
}
126126

127127
handleIcp4dUrlNotSet() {
128-
const notification = this.handleError('IBM Cloud Private for Data URL not specified',
128+
const notification = this.handleError('IBM Cloud Private for Data URL is not specified, is invalid, or is unreachable',
129129
{
130130
detail: 'Specify the IBM Cloud Private for Data URL or build with IBM Cloud Streaming Analytics in the build-ibmstreams package settings.',
131131
notificationButtons: [

lib/actions/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export const clearQueuedAction = () => ({
5858
type: actions.CLEAR_QUEUED_ACTION
5959
});
6060

61+
export const checkIcp4dHostExists = (successFn, errorFn) => ({
62+
type: actions.CHECK_ICP4D_HOST_EXISTS,
63+
successFn,
64+
errorFn
65+
});
66+
6167
export const authenticateIcp4d = (username, password, rememberPassword) => ({
6268
type: actions.AUTHENTICATE_ICP4D,
6369
username,
@@ -243,6 +249,7 @@ export const actions = {
243249
SET_PASSWORD: 'SET_PASSWORD',
244250
SET_REMEMBER_PASSWORD: 'SET_REMEMBER_PASSWORD',
245251
SET_FORM_DATA_FIELD: 'SET_FORM_DATA_FIELD',
252+
CHECK_ICP4D_HOST_EXISTS: 'CHECK_ICP4D_HOST_EXISTS',
246253
AUTHENTICATE_ICP4D: 'AUTHENTICATE_ICP4D',
247254
AUTHENTICATE_STREAMS_INSTANCE: 'AUTHENTICATE_STREAMS_INSTANCE',
248255
SET_ICP4D_AUTH_TOKEN: 'SET_ICP4D_AUTH_TOKEN',
@@ -286,6 +293,7 @@ export const actions = {
286293

287294
POST_PACKAGE_ACTIVATED: 'POST_PACKAGE_ACTIVATED',
288295
POST_ERROR: 'POST_ERROR',
296+
POST_CHECK_ICP4D_HOST_EXISTS: 'POST_CHECK_ICP4D_HOST_EXISTS',
289297
POST_GET_BUILD_ARTIFACTS_FULFILLED: 'POST_GET_BUILD_ARTIFACTS_FULFILLED',
290298
POST_DOWNLOAD_ARTIFACTS: 'POST_DOWNLOAD_ARTIFACTS',
291299
POST_SUBMIT_APPLICATIONS: 'POST_SUBMIT_APPLICATIONS',

lib/epics/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,19 @@ const openStreamsConsoleEpic = (action, state) => action.pipe(
322322
map(() => ({ type: actions.POST_OPEN_STREAMS_CONSOLE }))
323323
);
324324

325+
const icp4dHostExistsEpic = (action, state) => action.pipe(
326+
ofType(actions.CHECK_ICP4D_HOST_EXISTS),
327+
withLatestFrom(state),
328+
mergeMap(([a, s]) => StreamsRestUtils.icp4d.icp4dHostExists(s).pipe(
329+
tap((response) => a.successFn()),
330+
catchError(error => {
331+
a.errorFn();
332+
return of(handleError(a, error));
333+
})
334+
)),
335+
map(() => ({ type: actions.POST_CHECK_ICP4D_HOST_EXISTS }))
336+
);
337+
325338
const icp4dAuthEpic = (action, state) => action.pipe(
326339
ofType(actions.AUTHENTICATE_ICP4D),
327340
withLatestFrom(state),
@@ -474,6 +487,7 @@ const rootEpic = combineEpics(
474487
openStreamsConsoleEpic,
475488

476489
instanceSelectedEpic,
490+
icp4dHostExistsEpic,
477491
icp4dAuthEpic,
478492
streamsAuthEpic,
479493
getStreamsInstancesEpic,

lib/reducers/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,18 @@ const streamsV5Build = (state = [], action) => {
130130
streamsAuthError: action.authError
131131
};
132132
case actions.RESET_AUTH:
133-
return _.omit(state, [
134-
'icp4dAuthToken',
135-
'icp4dAuthError',
136-
'streamsInstances',
137-
'selectedInstance',
138-
'streamsAuthError'
139-
]);
133+
return {
134+
..._.omit(state, [
135+
'currentLoginStep',
136+
'icp4dAuthToken',
137+
'icp4dAuthError',
138+
'streamsInstances',
139+
'selectedInstance',
140+
'streamsAuthError',
141+
'username'
142+
]),
143+
currentLoginStep: 1
144+
};
140145
case actions.NEW_BUILD:
141146
return {
142147
...state,

lib/spl-build.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
setUseIcp4dMasterNodeHost,
3333
resetAuth,
3434
packageActivated,
35-
refreshToolkits
35+
refreshToolkits,
36+
checkIcp4dHostExists
3637
} from './actions';
3738
import getStore from './redux-store/configure-store';
3839
import StateSelector from './util/state-selectors';
@@ -405,9 +406,7 @@ export default {
405406
if (selectedComp) {
406407
this.mainCompositeSelectorPanel.hide();
407408
if (this.apiVersion === CONF_API_VERSION_V5) {
408-
// todo
409409
const fqn = this.namespace ? `${this.namespace}::${selectedComp}` : `${selectedComp}`;
410-
// const appRoot = SourceArchiveUtils.getApplicationRoot(atom.project.getPaths(), selectedFilePath);
411410
const toolkitRootPath = atom.config.get(CONF_TOOLKITS_PATH);
412411
let messageHandler = MessageHandlerRegistry.get(fqn);
413412
if (!messageHandler) {
@@ -473,11 +472,7 @@ export default {
473472

474473
buildMake(action) {
475474
if (this.apiVersion === CONF_API_VERSION_V5) {
476-
if (StateSelector.getIcp4dUrl(getStore().getState())) {
477-
this.buildMakeV5(action);
478-
} else {
479-
this.handleIcp4durlNotSet();
480-
}
475+
this.handleV5Action(() => this.buildMakeV5(action));
481476
} else {
482477
this.buildMakeV4(action);
483478
}
@@ -555,11 +550,7 @@ export default {
555550

556551
buildApp(action) {
557552
if (this.apiVersion === CONF_API_VERSION_V5) {
558-
if (StateSelector.getIcp4dUrl(getStore().getState())) {
559-
this.buildAppV5(action);
560-
} else {
561-
this.handleIcp4dUrlNotSet();
562-
}
553+
this.handleV5Action(() => this.buildAppV5(action));
563554
} else {
564555
this.buildAppV4(action);
565556
}
@@ -705,11 +696,7 @@ export default {
705696
*/
706697
submit() {
707698
if (this.apiVersion === CONF_API_VERSION_V5) {
708-
if (StateSelector.getIcp4dUrl(getStore().getState())) {
709-
this.submitV5();
710-
} else {
711-
this.handleIcp4dUrlNotSet();
712-
}
699+
this.handleV5Action(() => this.submitV5());
713700
} else {
714701
this.submitV4();
715702
}
@@ -732,17 +719,16 @@ export default {
732719

733720
openConsole() {
734721
if (this.apiVersion === CONF_API_VERSION_V5) {
735-
if (StateSelector.getIcp4dUrl(getStore().getState())) {
722+
const openConsoleFn = () => {
736723
const consoleUrlString = StateSelector.getStreamsConsoleUrl(getStore().getState());
737724
if (consoleUrlString) {
738725
try {
739726
const consoleUrl = new URL(consoleUrlString); /* eslint-disable-line compat/compat */
740727
MessageHandlerRegistry.openUrl(`${consoleUrl}`);
741728
} catch (err) { /* */ }
742729
}
743-
} else {
744-
this.handleIcp4dUrlNotSet();
745-
}
730+
};
731+
this.handleV5Action(openConsoleFn);
746732
} else {
747733
this.streamingAnalyticsCredentials = atom.config.get(CONF_STREAMING_ANALYTICS_CREDENTIALS);
748734
this.consoleService = this.consumeConsoleService({ id: name, name });
@@ -765,15 +751,24 @@ export default {
765751

766752
openIcp4dDashboard() {
767753
if (this.apiVersion === CONF_API_VERSION_V5) {
768-
const icp4dUrlString = StateSelector.getIcp4dUrl(getStore().getState());
769-
if (icp4dUrlString) {
754+
const openDashboard = () => {
770755
try {
771-
const icp4dUrl = new URL(icp4dUrlString); /* eslint-disable-line compat/compat */
756+
const icp4dUrl = new URL(StateSelector.getIcp4dUrl(getStore().getState())); /* eslint-disable-line compat/compat */
772757
MessageHandlerRegistry.openUrl(`${icp4dUrl}/zen/#/homepage`);
773758
} catch (err) { /* */ }
774-
} else {
775-
this.handleIcp4dUrlNotSet();
776-
}
759+
};
760+
this.handleV5Action(openDashboard);
761+
}
762+
},
763+
764+
handleV5Action(callbackFn) {
765+
const icp4dUrl = StateSelector.getIcp4dUrl(getStore().getState());
766+
if (icp4dUrl) {
767+
const successFn = callbackFn;
768+
const errorFn = () => this.handleIcp4dUrlNotSet(this.handleV5Action.bind(this, callbackFn));
769+
getStore().dispatch(checkIcp4dHostExists(successFn, errorFn));
770+
} else {
771+
this.handleIcp4dUrlNotSet(this.handleV5Action.bind(this, callbackFn));
777772
}
778773
},
779774

lib/util/streams-rest-v5.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ function getToolkitIndex(state, toolkitId) {
332332
* Helper functions
333333
*/
334334

335+
function icp4dHostExists(state) {
336+
const options = {
337+
method: 'HEAD',
338+
url: StateSelector.getIcp4dUrl(state),
339+
timeout: 2000
340+
};
341+
return observableRequest(baseRequest, options);
342+
}
343+
335344
function getIcp4dToken(state, username, password) {
336345
const options = {
337346
method: 'POST',
@@ -430,6 +439,7 @@ const toolkit = {
430439
};
431440

432441
const icp4d = {
442+
icp4dHostExists,
433443
getServiceInstances,
434444
getIcp4dToken,
435445
getStreamsAuthToken

0 commit comments

Comments
 (0)