mattermost-community / mattermost-app-zendesk

Zendesk App for Mattermost
13 stars 10 forks source link

Simplify error handling for error call responses #92

Open mickmister opened 2 years ago

mickmister commented 2 years ago

There are lots several 7-line blocks in src/restapi/fConnect.ts for handling errors:

https://github.com/mattermost/mattermost-app-zendesk/blob/3b94866d2f8e1e0450515d47f3817db55c7b4785/src/restapi/fConnect.ts#L114-L120

This could be made into a one-liner by using a helper function:

await tryCallResponseWithMessage(ppClient.storeOauth2User(storedToken), 'fOauth2Complete - Unable to store oauth2user', res);

which could be implemented as

export async function tryCallResponseWithMessage(promise, message, res) {
    return promise.catch((error) => {
        const fullMessage = message + ': ' + error.message;
        const callResponse = newErrorCallResponseWithMessage(fullMessage);
        res.json(callResponse);
    });
}

This improvement would make the code a lot easier to read. This can probably be applied to many places of the application.

bdoodo commented 2 years ago

Working on it

hanzei commented 2 years ago

Awesome, thanks you @bdoodo :tada: