Skip to content

Commit 0e8a127

Browse files
committed
feat: apply manifest - refactor
1 parent 9f4bb15 commit 0e8a127

File tree

3 files changed

+76
-40
lines changed

3 files changed

+76
-40
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { commands } from "vscode";
2+
3+
import { getFirstMetadataValue } from "@api";
4+
import { Code, ConnectError } from "@connectrpc/connect";
5+
import { vsCommands, SUPPORT_EMAIL } from "@constants";
6+
import { translate } from "@i18n";
7+
8+
/**
9+
* Handles ConnectError by displaying appropriate error messages
10+
* @param error The ConnectError to handle
11+
* @param namespace Optional namespace for generic error messages
12+
* @returns true if error was handled and caller should return, false if error should be ignored (e.g., AlreadyExists)
13+
*/
14+
export const handleConnectError = (error: ConnectError, namespace?: string): boolean => {
15+
const errorType = getFirstMetadataValue(error, "x-error-type");
16+
17+
if (error.code === Code.AlreadyExists) {
18+
// Resource already exists, caller should continue
19+
return false;
20+
}
21+
22+
if (errorType === "quota_limit_exceeded") {
23+
const quotaLimit = getFirstMetadataValue(error, "x-quota-limit");
24+
const quotaLimitUsed = getFirstMetadataValue(error, "x-quota-used");
25+
const quotaLimitResource = getFirstMetadataValue(error, "x-quota-resource");
26+
27+
commands.executeCommand(
28+
vsCommands.showErrorMessage,
29+
translate().t("errors.quotaLimitExceeded", {
30+
limit: quotaLimit,
31+
used: quotaLimitUsed,
32+
resource: quotaLimitResource,
33+
email: SUPPORT_EMAIL,
34+
})
35+
);
36+
return true;
37+
}
38+
39+
if (errorType === "rate_limit_exceeded") {
40+
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.rateLimitExceeded"));
41+
return true;
42+
}
43+
44+
if (error.code === Code.ResourceExhausted) {
45+
commands.executeCommand(
46+
vsCommands.showErrorMessage,
47+
translate().t("errors.resourceExhausted", { email: SUPPORT_EMAIL })
48+
);
49+
return true;
50+
}
51+
52+
if (error.code === Code.Unauthenticated) {
53+
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.unauthenticated"));
54+
return true;
55+
}
56+
57+
if (error.code === Code.PermissionDenied) {
58+
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.permissionDenied"));
59+
return true;
60+
}
61+
62+
// Generic error handling
63+
if (namespace) {
64+
commands.executeCommand(vsCommands.showErrorMessage, namespace, error.message);
65+
} else {
66+
commands.executeCommand(vsCommands.showErrorMessage, error.message);
67+
}
68+
return true;
69+
};

src/utilities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export {
1818
export { omit } from "@utilities/omit.utils";
1919
export { errorMessageWithLog } from "@utilities/messages.utils";
2020
export { getBaseURL, getWebUIURL, getOrganizationId } from "@utilities/url.utilities";
21+
export { handleConnectError } from "@utilities/errorHandler.utils";

src/vscommands/applyManifest.vscommands.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { commands, window } from "vscode";
22
import { parse as parseYaml } from "yaml";
33

4-
import { getFirstMetadataValue } from "@api";
5-
import { Code, ConnectError } from "@connectrpc/connect";
6-
import { namespaces, vsCommands, SUPPORT_EMAIL } from "@constants";
4+
import { ConnectError } from "@connectrpc/connect";
5+
import { namespaces, vsCommands } from "@constants";
76
import { getLocalResources } from "@controllers/utilities";
87
import { translate } from "@i18n";
98
import { LoggerService, ManifestService, ProjectsService } from "@services";
10-
import { getDirectoryOfFile, WorkspaceConfig } from "@utilities";
9+
import { getDirectoryOfFile, WorkspaceConfig, handleConnectError } from "@utilities";
1110

1211
export const applyManifest = async () => {
1312
if (!window.activeTextEditor) {
@@ -37,44 +36,11 @@ export const applyManifest = async () => {
3736

3837
if (createError) {
3938
if (createError instanceof ConnectError) {
40-
const errorType = getFirstMetadataValue(createError, "x-error-type");
41-
42-
if (createError.code === Code.AlreadyExists) {
43-
// Project already exists, continue to apply manifest
44-
} else if (errorType === "quota_limit_exceeded") {
45-
const quotaLimit = getFirstMetadataValue(createError, "x-quota-limit");
46-
const quotaLimitUsed = getFirstMetadataValue(createError, "x-quota-used");
47-
const quotaLimitResource = getFirstMetadataValue(createError, "x-quota-resource");
48-
49-
commands.executeCommand(
50-
vsCommands.showErrorMessage,
51-
translate().t("errors.quotaLimitExceeded", {
52-
limit: quotaLimit,
53-
used: quotaLimitUsed,
54-
resource: quotaLimitResource,
55-
email: SUPPORT_EMAIL,
56-
})
57-
);
58-
return;
59-
} else if (errorType === "rate_limit_exceeded") {
60-
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.rateLimitExceeded"));
61-
return;
62-
} else if (createError.code === Code.ResourceExhausted) {
63-
commands.executeCommand(
64-
vsCommands.showErrorMessage,
65-
translate().t("errors.resourceExhausted", { email: SUPPORT_EMAIL })
66-
);
67-
return;
68-
} else if (createError.code === Code.Unauthenticated) {
69-
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.unauthenticated"));
70-
return;
71-
} else if (createError.code === Code.PermissionDenied) {
72-
commands.executeCommand(vsCommands.showErrorMessage, translate().t("errors.permissionDenied"));
73-
return;
74-
} else {
75-
commands.executeCommand(vsCommands.showErrorMessage, namespaces.applyManifest, createError.message);
39+
const shouldReturn = handleConnectError(createError, namespaces.applyManifest);
40+
if (shouldReturn) {
7641
return;
7742
}
43+
// If handleConnectError returns false, it means Code.AlreadyExists - continue to apply manifest
7844
} else {
7945
// Non-ConnectError
8046
commands.executeCommand(vsCommands.showErrorMessage, namespaces.applyManifest, (createError as Error).message);

0 commit comments

Comments
 (0)