Skip to content

Commit

Permalink
[MI-2669]: Added some key-values in plugin.json file and created sepa…
Browse files Browse the repository at this point in the history
…rate functions for custom error components (#30)

* Added some key-values in plugin.json file and created separate functions for custom error components

* [MI-2669]: Added block lines

---------

Co-authored-by: Abhishek Verma <[email protected]>
  • Loading branch information
avas27JTG and avas27JTG authored Jan 31, 2023
1 parent 968391b commit 84596b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
3 changes: 3 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"name": "Azure DevOps",
"description": "This plugin provides services of Azure DevOps such as Boards and Repos",
"icon_path": "public/assets/azurebot.svg",
"homepage_url": "https://github.com/mattermost/mattermost-plugin-azure-devops",
"support_url": "https://github.com/mattermost/mattermost-plugin-azure-devops/issues",
"release_notes_url": "https://github.com/mattermost/mattermost-plugin-azure-devops/releases",
"version": "3.1.0",
"min_server_version": "5.37.0",
"server": {
Expand Down
82 changes: 48 additions & 34 deletions webapp/src/utils/errorHandling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import pluginConstants from 'pluginConstants';

const getSubscribeModalCustomError = (errorState?: ApiErrorResponse) => {
if (errorState?.status === 400 && errorState?.data.Error === pluginConstants.messages.error.subscriptionAlreadyExists) {
return pluginConstants.messages.error.subscriptionAlreadyExists;
}

if (errorState?.status === 403 && errorState?.data.Error.includes(pluginConstants.messages.error.accessDenied)) {
return pluginConstants.messages.error.adminAccessError;
}

if (errorState?.status === 500 && errorState?.data.Error.includes(pluginConstants.messages.error.failedToGetSubscriptions)) {
return pluginConstants.messages.error.failedToGetSubscriptions;
}

return null;
};

const getLinkProjectModalCustomError = (errorState?: ApiErrorResponse) => {
if (errorState?.status === 404 || errorState?.status === 401) {
return pluginConstants.messages.error.notAccessibleError;
}

if (errorState?.status === 500 && errorState?.data.Error.includes(pluginConstants.messages.error.errorExpectedForOAuthNotEnabled)) {
return pluginConstants.messages.error.errorMessageOAuthNotEnabled;
}

return null;
};

const confirmationModalCustomError = (errorState?: ApiErrorResponse) => {
if (errorState?.status === 403 && errorState?.data.Error.includes(pluginConstants.messages.error.accessDenied)) {
return pluginConstants.messages.error.adminAccessError;
}

if (errorState?.status === 404 && errorState?.data.Error.includes(pluginConstants.messages.error.subscriptionNotFound)) {
return pluginConstants.messages.error.subscriptionNotFound;
}

return null;
};

const customErrorAndComponentMap: Partial<Record<ErrorComponents, (errorState?: ApiErrorResponse) => string | null>> = {
SubscribeModal: (errorState?: ApiErrorResponse) => getSubscribeModalCustomError(errorState),
LinkProjectModal: (errorState?: ApiErrorResponse) => getLinkProjectModalCustomError(errorState),
ConfirmationModal: (errorState?: ApiErrorResponse) => confirmationModalCustomError(errorState),
};

const getErrorMessage = (
isError: boolean,
component: ErrorComponents,
Expand All @@ -9,40 +55,8 @@ const getErrorMessage = (
return '';
}

switch (component) {
case 'SubscribeModal': // Create subscription modal
if (errorState?.status === 400 && errorState?.data.Error === pluginConstants.messages.error.subscriptionAlreadyExists) {
return pluginConstants.messages.error.subscriptionAlreadyExists;
}
if (errorState?.status === 403 && errorState?.data.Error.includes(pluginConstants.messages.error.accessDenied)) {
return pluginConstants.messages.error.adminAccessError;
}
if (errorState?.status === 500 && errorState?.data.Error.includes(pluginConstants.messages.error.failedToGetSubscriptions)) {
return pluginConstants.messages.error.failedToGetSubscriptions;
}
return errorState?.data.Error ?? pluginConstants.messages.error.generic;

case 'LinkProjectModal':
if (errorState?.status === 404 || errorState?.status === 401) {
return pluginConstants.messages.error.notAccessibleError;
}
if (errorState?.status === 500 && errorState?.data.Error.includes(pluginConstants.messages.error.errorExpectedForOAuthNotEnabled)) {
return pluginConstants.messages.error.errorMessageOAuthNotEnabled;
}
return errorState?.data.Error ?? pluginConstants.messages.error.generic;

case 'ConfirmationModal':
if (errorState?.status === 403 && errorState?.data.Error.includes(pluginConstants.messages.error.accessDenied)) {
return pluginConstants.messages.error.adminAccessError;
}
if (errorState?.status === 404 && errorState?.data.Error.includes(pluginConstants.messages.error.subscriptionNotFound)) {
return pluginConstants.messages.error.subscriptionNotFound;
}
return pluginConstants.messages.error.generic;

default:
return errorState?.data.Error ?? pluginConstants.messages.error.generic;
}
// Return custom or API error for different components
return customErrorAndComponentMap[component]?.(errorState) ?? errorState?.data.Error ?? pluginConstants.messages.error.generic;
};

export default getErrorMessage;

0 comments on commit 84596b6

Please sign in to comment.