Skip to content

Commit

Permalink
fix: try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanurazvan committed Apr 13, 2023
1 parent b5cca71 commit 8672f0e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 98 deletions.
14 changes: 0 additions & 14 deletions electron/app/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ export async function getGitRepoInfo(localPath: string) {

let gitRepo: GitRepo;

try {
const [remoteBranchSummary, localBranches, remoteUrl] = await Promise.all([
git.branch({'-r': null}),
git.branchLocal(),
getGitRemoteUrl(localPath),
]);

console.log('remoteBranchSummary', remoteBranchSummary);
console.log('localBranches', localBranches);
console.log('remoteUrl', remoteUrl);
} catch (e: any) {
throw new Error(e.message);
}

try {
const remoteBranchSummary = await git.branch({'-r': null});
const localBranches = await git.branchLocal();
Expand Down
19 changes: 10 additions & 9 deletions src/components/molecules/BranchSelect/BranchCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ const BranchCell: React.FC<IProps> = props => {
onOk() {
dispatch(setGitLoading(true));

try {
deleteLocalBranch({localPath: selectedProjectRootFolder, branchName: branch.name});
dispatch(
setAlert({type: AlertEnum.Success, title: `Branch ${branch.name} deleted successfully`, message: ''})
);
} catch (err) {
dispatch(setGitLoading(false));
showGitErrorModal(`Deleting ${branch.name} failed`, undefined, `git branch -d ${branch.name}`, dispatch);
}
deleteLocalBranch({localPath: selectedProjectRootFolder, branchName: branch.name})
.then(() => {
dispatch(
setAlert({type: AlertEnum.Success, title: `Branch ${branch.name} deleted successfully`, message: ''})
);
})
.catch(() => {
dispatch(setGitLoading(false));
showGitErrorModal(`Deleting ${branch.name} failed`, undefined, `git branch -d ${branch.name}`, dispatch);
});
},
onCancel() {},
zIndex: 100000,
Expand Down
10 changes: 5 additions & 5 deletions src/components/molecules/BranchSelect/BranchSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ function BranchSelect() {
(branch: GitBranch) => {
const branchName = branch.type === 'local' ? branch.name : branch.name.replace('origin/', '');

try {
checkoutGitBranch({localPath: rootFolderPath, branchName}).then(() => {
checkoutGitBranch({localPath: rootFolderPath, branchName})
.then(() => {
dispatch(setCurrentBranch(branchName));
setVisible(false);
})
.catch(() => {
showGitErrorModal('Checkout failed', undefined, `git checkout -b ${branchName}`, dispatch);
});
} catch (e) {
showGitErrorModal('Checkout failed', undefined, `git checkout -b ${branchName}`, dispatch);
}
},
[rootFolderPath, dispatch]
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/organisms/GitCloneModal/GitCloneModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Button, Form, Input, Modal} from 'antd';
import {useForm} from 'antd/lib/form/Form';

import fs from 'fs';
import {rm} from 'fs/promises';
import {sep} from 'path';
import styled from 'styled-components';

Expand Down Expand Up @@ -71,7 +72,7 @@ const GitCloneModal: React.FC = () => {
});

if (doesPathExist(localGitPath)) {
fs.rmdirSync(localGitPath, {recursive: true});
rm(localGitPath, {recursive: true});
}
}
});
Expand Down
10 changes: 5 additions & 5 deletions src/components/organisms/GitPane/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ const FileList: React.FC<IProps> = props => {
{
key: 'stage_unstage_changes',
label: item.status === 'staged' ? 'Unstage changes' : 'Stage changes',
onClick: () => {
onClick: async () => {
if (!selectedProjectRootFolder) return;

dispatch(setGitLoading(true));

if (item.status === 'unstaged') {
try {
stageChangedFiles({localPath: selectedProjectRootFolder, filePaths: [item.fullGitPath]});
await stageChangedFiles({localPath: selectedProjectRootFolder, filePaths: [item.fullGitPath]});
} catch (e) {
showGitErrorModal(
'Staging changes failed!',
Expand All @@ -83,7 +83,7 @@ const FileList: React.FC<IProps> = props => {
}
} else {
try {
unstageFiles({localPath: selectedProjectRootFolder, filePaths: [item.fullGitPath]});
await unstageFiles({localPath: selectedProjectRootFolder, filePaths: [item.fullGitPath]});
} catch (e) {
showGitErrorModal(
'Unstage changes failed!',
Expand All @@ -104,14 +104,14 @@ const FileList: React.FC<IProps> = props => {
onClick: () => {
Modal.confirm({
title: discardTitle(item),
onOk() {
async onOk() {
dispatch(setGitLoading(true));

try {
if (item.type === 'modified') {
dispatch(updateFileEntry({path: item.path, text: item.originalContent}));
} else if (item.type === 'added' || item.type === 'untracked') {
deleteFile(item.fullGitPath);
await deleteFile(item.fullGitPath);
} else if (item.type === 'deleted') {
createFileWithContent(item.fullGitPath, item.originalContent);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/organisms/PageHeader/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const PageHeader = () => {
monitorGitFolder(projectRootFolder, store);

try {
getRepoInfo({path: projectRootFolder || ''}).then(repo => {
await getRepoInfo({path: projectRootFolder || ''}).then(repo => {
dispatch(setRepo(repo));
dispatch(setCurrentBranch(repo.currentBranch));
setIsInitializingGitRepo(false);
Expand Down
44 changes: 22 additions & 22 deletions src/redux/services/fileMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,28 @@ export function monitorRootFolder(folder: string, thunkAPI: {getState: Function;
const paths: Array<string> = args.map(arg => arg[0]);
thunkAPI.dispatch(multiplePathsRemoved(filterGitFolder(paths)));

try {
getRepoInfo({path: folder}).then(repo => {
getRepoInfo({path: folder})
.then(repo => {
if (!thunkAPI.getState().git.loading) {
thunkAPI.dispatch(setGitLoading(true));
}

thunkAPI.dispatch(setRepo(repo));
thunkAPI.dispatch(setCurrentBranch(repo.currentBranch));

try {
getChangedFiles({localPath: folder, fileMap: thunkAPI.getState().main.fileMap}).then(changedFiles => {
getChangedFiles({localPath: folder, fileMap: thunkAPI.getState().main.fileMap})
.then(changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
})
.catch(() => {
thunkAPI.dispatch(setGitLoading(false));
});
} catch (e) {
thunkAPI.dispatch(setGitLoading(false));
}
})
.catch((err: any) => {
showGitErrorModal('Git repository error', err.message);
thunkAPI.dispatch(setRepo(undefined));
});
} catch (e: any) {
showGitErrorModal('Git repository error', e.message);
thunkAPI.dispatch(setRepo(undefined));
}
}, 1000)
)
.on(
Expand All @@ -88,28 +88,28 @@ export function monitorRootFolder(folder: string, thunkAPI: {getState: Function;
const paths: Array<string> = args.map(arg => arg[0]);
thunkAPI.dispatch(multiplePathsRemoved(filterGitFolder(paths)));

try {
getRepoInfo({path: folder}).then(repo => {
getRepoInfo({path: folder})
.then(repo => {
if (!thunkAPI.getState().git.loading) {
thunkAPI.dispatch(setGitLoading(true));
}

thunkAPI.dispatch(setRepo(repo));
thunkAPI.dispatch(setCurrentBranch(repo.currentBranch));

try {
getChangedFiles({localPath: folder, fileMap: thunkAPI.getState().main.fileMap}).then(changedFiles => {
getChangedFiles({localPath: folder, fileMap: thunkAPI.getState().main.fileMap})
.then(changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
})
.catch(() => {
thunkAPI.dispatch(setGitLoading(false));
});
} catch (e) {
thunkAPI.dispatch(setGitLoading(false));
}
})
.catch((err: any) => {
showGitErrorModal('Git repository error', err.message);
thunkAPI.dispatch(setRepo(undefined));
});
} catch (e: any) {
showGitErrorModal('Git repository error', e.message);
thunkAPI.dispatch(setRepo(undefined));
}
}, 1000)
);

Expand Down
42 changes: 21 additions & 21 deletions src/redux/services/gitFolderMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,51 +68,51 @@ export async function monitorGitFolder(rootFolderPath: string | null, thunkAPI:
const absolutePathEndsWithGit = absolutePath.endsWith(`${sep}.git`);

if (path.startsWith(`${absolutePath}${absolutePathEndsWithGit ? '' : `${sep}.git`}${sep}FETCH_HEAD`)) {
try {
getRepoInfo({path: rootFolderPath}).then(repo => {
getRepoInfo({path: rootFolderPath})
.then(repo => {
thunkAPI.dispatch(setRepo(repo));
})
.catch((err: any) => {
thunkAPI.dispatch(setRepo(undefined));
showGitErrorModal('Git repository error', err.message);
});
} catch (e: any) {
thunkAPI.dispatch(setRepo(undefined));
showGitErrorModal('Git repository error', e.message);
}
}

// commit was made/undoed or push was made
if (path.startsWith(`${absolutePath}${absolutePathEndsWithGit ? '' : `${sep}.git`}${sep}logs${sep}refs`)) {
const branchName = path.includes('heads') ? gitRepo.currentBranch : `origin/${gitRepo.currentBranch}`;

try {
getAheadBehindCommitsCount({localPath: rootFolderPath, branchName}).then(({aheadCount, behindCount}) => {
getAheadBehindCommitsCount({localPath: rootFolderPath, branchName})
.then(({aheadCount, behindCount}) => {
thunkAPI.dispatch(setCommitsCount({aheadCount, behindCount}));
})
.catch(() => {
thunkAPI.dispatch(setCommitsCount({aheadCount: 0, behindCount: 0}));
});
} catch (e) {
thunkAPI.dispatch(setCommitsCount({aheadCount: 0, behindCount: 0}));
}

try {
getBranchCommits({localPath: rootFolderPath, branchName}).then(commits => {
getBranchCommits({localPath: rootFolderPath, branchName})
.then(commits => {
thunkAPI.dispatch(setBranchCommits({branchName, commits}));
})
.catch(() => {
thunkAPI.dispatch(setBranchCommits({branchName, commits: []}));
});
} catch (e) {
thunkAPI.dispatch(setBranchCommits({branchName, commits: []}));
}
}
})
.on('unlinkDir', () => {
const rootFolder = thunkAPI.getState().config.selectedProjectRootFolder;
const repo = thunkAPI.getState().git.repo;

try {
isFolderGitRepo({path: rootFolder}).then(isRepo => {
isFolderGitRepo({path: rootFolder})
.then(isRepo => {
if (!isRepo && repo) {
thunkAPI.dispatch(setChangedFiles([]));
thunkAPI.dispatch(setRepo(undefined));
thunkAPI.dispatch(updateProjectsGitRepo([{path: rootFolderPath || '', isGitRepo: false}]));
}
})
.catch(err => {
log.error(err);
});
} catch (err) {
log.error(err);
}
});
}
18 changes: 8 additions & 10 deletions src/redux/thunks/multiplePathsAdded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ export const multiplePathsAdded = createAsyncThunk<
thunkAPI.dispatch(setGitLoading(true));
}

try {
getChangedFiles({localPath: projectRootFolder || '', fileMap: thunkAPI.getState().main.fileMap}).then(
changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
}
);
} catch (e) {
thunkAPI.dispatch(setGitLoading(false));
}
getChangedFiles({localPath: projectRootFolder || '', fileMap: thunkAPI.getState().main.fileMap})
.then(changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
})
.catch(() => {
thunkAPI.dispatch(setGitLoading(false));
});
}

return {
Expand Down
18 changes: 8 additions & 10 deletions src/redux/thunks/multiplePathsChanged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ export const multiplePathsChanged = createAsyncThunk<
thunkAPI.dispatch(setGitLoading(true));
}

try {
getChangedFiles({localPath: projectRootFolder || '', fileMap: thunkAPI.getState().main.fileMap}).then(
changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
}
);
} catch (e) {
thunkAPI.dispatch(setGitLoading(false));
}
getChangedFiles({localPath: projectRootFolder || '', fileMap: thunkAPI.getState().main.fileMap})
.then(changedFiles => {
thunkAPI.dispatch(setChangedFiles(changedFiles));
thunkAPI.dispatch(setGitLoading(false));
})
.catch(() => {
thunkAPI.dispatch(setGitLoading(false));
});
}

return {
Expand Down

0 comments on commit 8672f0e

Please sign in to comment.