Skip to content

Commit 0946d3a

Browse files
fix: error handling, formatting and passing values to params
1 parent d77130d commit 0946d3a

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

src/commands/app/reinstall.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,10 @@ export default class Reinstall extends AppCLIBaseCommand {
105105
await reinstallApp({
106106
flags: this.flags,
107107
type: appType,
108-
orgUid: this.sharedConfig.org,
109-
manifestUid: this.manifestData.uid,
110-
configType: this.sharedConfig,
111108
developerHubBaseUrl: this.developerHubBaseUrl,
112-
});
109+
},
110+
{orgUid: this.sharedConfig.org, manifestUid: this.manifestData.uid}
111+
);
113112
this.log(
114113
$t(reinstallAppMsg.APP_REINSTALLED_SUCCESSFULLY, {
115114
app: app?.name || (this.flags["app-uid"] as string),

src/util/api-request-handler.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import { formatErrors } from "./error-helper";
33

44
interface RequestParams {
55
orgUid: string;
6-
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
6+
method: "GET" | "POST" | "PUT" | "DELETE";
77
queryParams?: Record<string, any>;
88
payload?: any;
99
url: string;
1010
}
1111

1212
export async function apiRequestHandler(params: RequestParams): Promise<any> {
1313
const { orgUid, method, queryParams, payload, url } = params;
14-
const authtoken = configHandler.get('authtoken');
14+
const authtoken = configHandler.get("authtoken");
1515

1616
const headers = {
1717
organization_uid: orgUid,
18-
authtoken
18+
authtoken,
1919
};
2020

2121
const httpClient = new HttpClient();
@@ -27,13 +27,13 @@ export async function apiRequestHandler(params: RequestParams): Promise<any> {
2727

2828
try {
2929
let response;
30-
if (method === 'GET') {
30+
if (method === "GET") {
3131
response = await httpClient.get(url);
32-
} else if (method === 'POST') {
32+
} else if (method === "POST") {
3333
response = await httpClient.post(url, payload);
34-
} else if (method === 'PUT') {
34+
} else if (method === "PUT") {
3535
response = await httpClient.put(url, payload);
36-
} else if (method === 'DELETE') {
36+
} else if (method === "DELETE") {
3737
response = await httpClient.delete(url);
3838
} else {
3939
throw new Error(`Unsupported HTTP method: ${method}`);
@@ -43,11 +43,10 @@ export async function apiRequestHandler(params: RequestParams): Promise<any> {
4343
if (status >= 200 && status < 300) {
4444
return data;
4545
}
46-
data?.error
47-
? formatErrors(data.error)
48-
: data?.error_message || 'Something went wrong';
49-
throw data;
50-
46+
const error_message = data?.error
47+
? formatErrors(data)
48+
: data?.error_message || "Something went wrong";
49+
throw error_message;
5150
} catch (error) {
5251
throw error;
5352
}

src/util/common-utils.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContentstackClient, FlagInput } from "@contentstack/cli-utilities";
2-
import { AppLocation, ConfigType, Extension, LogFn } from "../types";
2+
import { AppLocation, Extension, LogFn } from "../types";
33
import { cliux, Stack } from "@contentstack/cli-utilities";
44
import { apiRequestHandler } from "./api-request-handler";
55

@@ -138,13 +138,12 @@ function installApp(
138138
async function reinstallApp(params: {
139139
flags: FlagInput;
140140
type: string;
141-
orgUid: string;
142-
manifestUid: string;
143-
configType: ConfigType;
144141
developerHubBaseUrl: string;
145-
}): Promise<void> {
146-
const { type, orgUid, manifestUid, developerHubBaseUrl, flags } = params;
147-
142+
},
143+
headers: { orgUid: string, manifestUid: string }
144+
): Promise<void> {
145+
const { type, developerHubBaseUrl, flags } = params;
146+
const {orgUid, manifestUid } = headers;
148147
const payload = {
149148
target_type: type,
150149
target_uid: (flags["stack-api-key"] as any) || orgUid,
@@ -153,8 +152,7 @@ async function reinstallApp(params: {
153152
const url = `https://${developerHubBaseUrl}/manifests/${manifestUid}/reinstall`;
154153
try {
155154
const result = await apiRequestHandler({
156-
orgUid,
157-
queryParams: params,
155+
orgUid,
158156
payload,
159157
url,
160158
method: "PUT"

src/util/error-helper.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
export function formatErrors(errors: any): string {
2-
const errorMessages: string[] = [];
3-
for (const errorKey in errors) {
4-
const errorValue = errors[errorKey];
5-
if (Array.isArray(errorValue)) {
6-
errorMessages.push(...errorValue.map((error: any) => formatError(error)));
7-
} else {
8-
errorMessages.push(formatError(errorValue));
9-
}
2+
let errorMessage: string = "";
3+
if (errors.message) {
4+
errorMessage = errors.message;
105
}
11-
return errorMessages.join(" ");
12-
}
13-
function formatError(error: any): string {
14-
if (typeof error === "object") {
15-
return Object.values(error).join(" ");
16-
}
17-
return String(error);
6+
return errorMessage;
187
}

0 commit comments

Comments
 (0)