Skip to content

Commit

Permalink
Handle the HTTP errors coming from scripts and show a different messa…
Browse files Browse the repository at this point in the history
…ge if initial configuration
  • Loading branch information
Marc-Antoine Hinse committed Jan 15, 2025
1 parent 5728ccd commit 9ecb106
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
gap: 0.5rem;
align-items: center;
color: var(--error);
text-align: start;
}

.error-container[hidden] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const ConfigurationInitialStep: FC<{
setIsLoading(false);
onApiKeyValidated();
})
.catch((error: any) => {
setErrorMessage(error.data);
.catch((error: Error) => {
setErrorMessage(error.message);
setIsLoading(false);
toastManager.show({
id: ToastKeys.ERROR,
isError: true,
content: 'Something went wrong. Please review your form.',
content: `Something went wrong. ${error.message}`,
});
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/components/Toast.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
.toast-text {
color: var(--text-color);
overflow-wrap: anywhere;
text-align: start;
}

@keyframes toast-animation-in {
Expand Down
49 changes: 33 additions & 16 deletions packages/react-components/src/utils/setupConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,61 @@ function createService(): SplunkService {
return service;
}

function handleHTTPScriptError(error: any): Promise<boolean> {
if (error.status === 400) {
return fetchIsFirstConfiguration().then((isFirstConfiguration: boolean) => {
if (isFirstConfiguration) {
throw Error(
'The Splunk instance needs to be restarted to recognize the scripts of this application.'
);
} else if (error.data.messages !== undefined && error.data.messages.length > 0) {
throw Error(error.data.messages[0].text);
}
throw Error(error.data);
});
} else {
throw Error(error.data);
}
}

function fetchApiKeyValidation(apiKey: string): Promise<boolean> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_api_key_validation', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_api_key_validation', data)
.then((response: SplunkRequestResponse) => {
return response.status === 200;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchUserTenants(apiKey: string): Promise<Array<Tenant>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_user_tenants', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_user_tenants', data)
.then((response: SplunkRequestResponse) => {
return response.data.tenants;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchSeverityFilters(apiKey: string): Promise<Array<Severity>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_severity_filters', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_severity_filters', data)
.then((response: SplunkRequestResponse) => {
return response.data.severities;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchSourceTypeFilters(apiKey: string): Promise<Array<SourceTypeCategory>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_source_type_filters', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_source_type_filters', data)
.then((response: SplunkRequestResponse) => {
return response.data.categories;
}
);
})
.catch(handleHTTPScriptError);
}

function doesPasswordExist(storage: SplunkStoragePasswordAccessors, key: string): boolean {
Expand Down

0 comments on commit 9ecb106

Please sign in to comment.