Skip to content

Commit dd98bc6

Browse files
committed
refactor: error handling
1 parent 6e92997 commit dd98bc6

File tree

5 files changed

+166
-154
lines changed

5 files changed

+166
-154
lines changed

packages/insomnia-api/src/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const createTeamProject = ({
4040
organizationId: string;
4141
name: string;
4242
}) => {
43-
return fetch<{ id: string; name: string } | { error: string; message?: string }>({
43+
return fetch<{ id: string; name: string }>({
4444
method: 'POST',
4545
path: `/v1/organizations/${organizationId}/team-projects`,
4646
data: {

packages/insomnia/src/routes/organization.$organizationId.project.$projectId.delete.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { database } from '~/common/database';
55
import { projectLock } from '~/common/project';
66
import * as models from '~/models';
77
import { reportGitProjectCount } from '~/routes/organization.$organizationId.project.new';
8+
import { parseInsomniaFetchError } from '~/ui/insomnia-fetch';
89
import { invariant } from '~/utils/invariant';
910
import { createFetcherSubmitHook, getInitialRouteForOrganization } from '~/utils/router';
1011

@@ -25,20 +26,11 @@ export async function clientAction({ params }: Route.ClientActionArgs) {
2526
await projectLock.lock();
2627
const bufferId = await database.bufferChanges();
2728
if (project.remoteId) {
28-
const response = await deleteTeamProject({
29+
await deleteTeamProject({
2930
organizationId,
3031
projectRemoteId: project.remoteId,
3132
sessionId,
3233
});
33-
34-
if (response && 'error' in response) {
35-
return {
36-
error:
37-
response.error === 'FORBIDDEN'
38-
? 'You do not have permission to delete this project.'
39-
: 'An unexpected error occurred while deleting the project. Please try again.',
40-
};
41-
}
4234
}
4335

4436
if (project.gitRepositoryId) {
@@ -56,13 +48,16 @@ export async function clientAction({ params }: Route.ClientActionArgs) {
5648
// When redirect to `/organizations/:organizationId`, it sometimes doesn't reload the index loader, so manually redirect to the initial route for the organization
5749
const initialOrganizationRoute = await getInitialRouteForOrganization({ organizationId });
5850
return redirect(initialOrganizationRoute);
59-
} catch (err) {
60-
console.log(err);
51+
} catch (err: unknown) {
52+
const parsedError = parseInsomniaFetchError(err);
53+
console.log(parsedError);
54+
let errorMessage = '';
55+
errorMessage =
56+
parsedError.name === 'FORBIDDEN'
57+
? 'You do not have permission to delete this project.'
58+
: `An unexpected error occurred while deleting the project. Please try again.${parsedError?.message}`;
6159
return {
62-
error:
63-
err instanceof Error
64-
? err.message
65-
: `An unexpected error occurred while deleting the project. Please try again. ${err}`,
60+
error: errorMessage,
6661
};
6762
} finally {
6863
await projectLock.unlock();

packages/insomnia/src/routes/organization.$organizationId.project.$projectId.update.tsx

Lines changed: 83 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { WorkspaceMeta } from '~/models/workspace-meta';
99
import { reportGitProjectCount } from '~/routes/organization.$organizationId.project.new';
1010
import { SegmentEvent } from '~/ui/analytics';
1111
import { showToast } from '~/ui/components/toast-notification';
12+
import { parseInsomniaFetchError } from '~/ui/insomnia-fetch';
1213
import { invariant } from '~/utils/invariant';
1314
import { createFetcherSubmitHook } from '~/utils/router';
1415

@@ -43,36 +44,37 @@ export async function clientAction({ request, params }: Route.ClientActionArgs)
4344
await projectLock.lock();
4445
// If its a cloud project, and we are renaming, then patch
4546
if (sessionId && project.remoteId && storageType === 'remote' && name !== project.name) {
46-
const response = await updateTeamProject({
47-
organizationId: project.parentId,
48-
projectRemoteId: project.remoteId,
49-
sessionId,
50-
name,
51-
});
52-
53-
if (response && 'error' in response) {
54-
let error = 'An unexpected error occurred while updating your project. Please try again.';
55-
if (response.error === 'FORBIDDEN') {
56-
error = 'You do not have permission to create a cloud project in this organization.';
47+
try {
48+
await updateTeamProject({
49+
organizationId: project.parentId,
50+
projectRemoteId: project.remoteId,
51+
sessionId,
52+
name,
53+
});
54+
} catch (error: unknown) {
55+
const parsedError = parseInsomniaFetchError(error);
56+
let errorMessage = 'An unexpected error occurred while updating your project. Please try again.';
57+
if (parsedError.name === 'FORBIDDEN') {
58+
errorMessage = 'You do not have permission to create a cloud project in this organization.';
5759
}
5860

59-
if (response.error === 'NEEDS_TO_UPGRADE') {
60-
error = 'Upgrade your account in order to create new Cloud Projects.';
61+
if (parsedError.name === 'NEEDS_TO_UPGRADE') {
62+
errorMessage = 'Upgrade your account in order to create new Cloud Projects.';
6163
}
6264

63-
if (response.error === 'PROJECT_STORAGE_RESTRICTION') {
64-
error = 'The owner of the organization allows only Local Vault project creation, please try again.';
65+
if (parsedError.name === 'PROJECT_STORAGE_RESTRICTION') {
66+
errorMessage = 'The owner of the organization allows only Local Vault project creation, please try again.';
6567
}
6668

6769
showToast({
6870
title: 'Error updating project',
69-
description: error,
71+
description: errorMessage,
7072
icon: 'warning',
7173
status: 'error',
7274
});
7375

7476
return {
75-
error,
77+
error: errorMessage,
7678
};
7779
}
7880

@@ -90,41 +92,40 @@ export async function clientAction({ request, params }: Route.ClientActionArgs)
9092

9193
// convert from cloud to local
9294
if (storageType === 'local' && project.remoteId) {
93-
const response = await deleteTeamProject({
94-
organizationId,
95-
projectRemoteId: project.remoteId,
96-
sessionId,
97-
});
95+
try {
96+
await deleteTeamProject({
97+
organizationId,
98+
projectRemoteId: project.remoteId,
99+
sessionId,
100+
});
98101

99-
if (response && !response.error) {
100102
window.main.trackSegmentEvent({
101103
event: SegmentEvent.projectUpdated,
102104
properties: {
103105
storage: 'local',
104106
},
105107
});
106-
}
107-
108-
if (response && 'error' in response) {
109-
let error = 'An unexpected error occurred while updating your project. Please try again.';
108+
} catch (error: unknown) {
109+
const parsedError = parseInsomniaFetchError(error);
110+
let errorMessage = 'An unexpected error occurred while updating your project. Please try again.';
110111

111-
if (response.error === 'FORBIDDEN') {
112-
error = 'You do not have permission to change this project.';
112+
if (parsedError.name === 'FORBIDDEN') {
113+
errorMessage = 'You do not have permission to change this project.';
113114
}
114115

115-
if (response.error === 'PROJECT_STORAGE_RESTRICTION') {
116-
error = 'The owner of the organization allows only Cloud Sync project creation, please try again.';
116+
if (parsedError.name === 'PROJECT_STORAGE_RESTRICTION') {
117+
errorMessage = 'The owner of the organization allows only Cloud Sync project creation, please try again.';
117118
}
118119

119120
showToast({
120121
title: 'Error updating project',
121-
description: error,
122+
description: errorMessage,
122123
icon: 'warning',
123124
status: 'error',
124125
});
125126

126127
return {
127-
error,
128+
error: errorMessage,
128129
};
129130
}
130131

@@ -141,105 +142,101 @@ export async function clientAction({ request, params }: Route.ClientActionArgs)
141142
}
142143
// convert from local/git to cloud
143144
if (storageType === 'remote' && !project.remoteId) {
144-
const newCloudProject = await createTeamProject({
145-
sessionId,
146-
organizationId,
147-
name,
148-
});
145+
try {
146+
const newCloudProject = await createTeamProject({
147+
sessionId,
148+
organizationId,
149+
name,
150+
});
149151

150-
if (newCloudProject && !('error' in newCloudProject)) {
151152
window.main.trackSegmentEvent({
152153
event: SegmentEvent.projectUpdated,
153154
properties: {
154155
storage: 'remote',
155156
},
156157
});
157-
}
158158

159-
if (!newCloudProject || 'error' in newCloudProject) {
160-
let error = 'An unexpected error occurred while updating your project. Please try again.';
161-
if (newCloudProject.error === 'FORBIDDEN') {
162-
error = newCloudProject.error;
159+
if (project.gitRepositoryId) {
160+
const gitRepository = await models.gitRepository.getById(project.gitRepositoryId);
161+
162+
gitRepository && (await models.gitRepository.remove(gitRepository));
163163
}
164164

165-
if (newCloudProject.error === 'NEEDS_TO_UPGRADE') {
166-
error = 'Upgrade your account in order to create new Cloud Projects.';
165+
await models.project.update(project, { name, remoteId: newCloudProject.id, gitRepositoryId: null });
166+
167+
project.gitRepositoryId && reportGitProjectCount(organizationId, sessionId);
168+
169+
showToast({
170+
title: 'Project updated',
171+
status: 'success',
172+
});
173+
174+
return {
175+
success: true,
176+
};
177+
} catch (error: unknown) {
178+
const parsedError = parseInsomniaFetchError(error);
179+
let errorMessage = 'An unexpected error occurred while updating your project. Please try again.';
180+
if (parsedError.name === 'FORBIDDEN') {
181+
errorMessage = parsedError.message;
167182
}
168183

169-
if (newCloudProject.error === 'PROJECT_STORAGE_RESTRICTION') {
170-
error = 'The owner of the organization allows only Local Vault project creation, please try again.';
184+
if (parsedError.name === 'NEEDS_TO_UPGRADE') {
185+
errorMessage = 'Upgrade your account in order to create new Cloud Projects.';
186+
}
187+
if (parsedError.name === 'PROJECT_STORAGE_RESTRICTION') {
188+
errorMessage = 'The owner of the organization allows only Local Vault project creation, please try again.';
171189
}
172190

173191
showToast({
174192
title: 'Error updating project',
175-
description: error,
193+
description: errorMessage,
176194
icon: 'warning',
177195
status: 'error',
178196
});
179197

180198
return {
181-
error,
199+
error: errorMessage,
182200
};
183201
}
184-
185-
if (project.gitRepositoryId) {
186-
const gitRepository = await models.gitRepository.getById(project.gitRepositoryId);
187-
188-
gitRepository && (await models.gitRepository.remove(gitRepository));
189-
}
190-
191-
await models.project.update(project, { name, remoteId: newCloudProject.id, gitRepositoryId: null });
192-
193-
project.gitRepositoryId && reportGitProjectCount(organizationId, sessionId);
194-
195-
showToast({
196-
title: 'Project updated',
197-
status: 'success',
198-
});
199-
200-
return {
201-
success: true,
202-
};
203202
}
204203

205204
// convert to git
206205
if (storageType === 'git' && !project.gitRepositoryId) {
207206
if (project.remoteId) {
208-
const response = await deleteTeamProject({
209-
organizationId,
210-
projectRemoteId: project.remoteId,
211-
sessionId,
212-
});
207+
try {
208+
await deleteTeamProject({
209+
organizationId,
210+
projectRemoteId: project.remoteId,
211+
sessionId,
212+
});
213213

214-
if (response && !response.error) {
215214
window.main.trackSegmentEvent({
216215
event: SegmentEvent.projectUpdated,
217216
properties: {
218217
storage: 'git',
219218
},
220219
});
221-
}
222-
223-
if (response && 'error' in response) {
224-
let error = 'An unexpected error occurred while updating your project. Please try again.';
225-
226-
if (response.error === 'FORBIDDEN') {
227-
error = 'You do not have permission to change this project.';
220+
} catch (error: unknown) {
221+
let errorMessage = 'An unexpected error occurred while updating your project. Please try again.';
222+
const parsedError = parseInsomniaFetchError(error);
223+
if (parsedError.name === 'FORBIDDEN') {
224+
errorMessage = 'You do not have permission to change this project.';
228225
}
229226

230-
if (response.error === 'PROJECT_STORAGE_RESTRICTION') {
231-
error = 'The owner of the organization allows only Cloud Sync project creation, please try again.';
227+
if (parsedError.name === 'PROJECT_STORAGE_RESTRICTION') {
228+
errorMessage = 'The owner of the organization allows only Cloud Sync project creation, please try again.';
232229
}
233230

234231
showToast({
235232
title: 'Error updating project',
236-
description: error,
233+
description: errorMessage,
237234
icon: 'warning',
238235
status: 'error',
239236
});
240237

241238
return {
242-
error,
239+
error: errorMessage,
243240
};
244241
}
245242
}

0 commit comments

Comments
 (0)