Skip to content

Commit

Permalink
Fix error for empty command args (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifercr07 authored Oct 7, 2024
1 parent 7f75b7b commit 588f5ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lib/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('executeShellCommandOnServer', () => {

const result = await executeShellCommandOnServer(mockCmd, mockCmdOptions);

expect(result).toBe('Error: Error: Unexpected response structure');
expect(result).toBe('Error: Unexpected response structure');
expect(WebService.post).toHaveBeenCalledWith(
mockCmdExecURL,
mockCmdOptions,
Expand All @@ -58,7 +58,7 @@ describe('executeShellCommandOnServer', () => {
mockCmdExecURL,
mockCmdOptions,
);
expect(result).toBe(`Error: ${mockError}`);
expect(result).toBe(`${mockError}`);
consoleErrorSpy.mockRestore();
});

Expand Down
9 changes: 7 additions & 2 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ export const executeShellCommandOnServer = async (

try {
const response = await WebService.post(cmdExecURL, cmdOptions);

// Check if the response contains data or if it's an error response
if (response?.data) {
return response.data;
} else if (response?.error) {
return response.error;
} else {
throw new Error('Unexpected response structure');
}
} catch (error: unknown) {
} catch (error) {
// Propagate the error from the backend exactly as it is
console.error('Error executing command:', error);
return `Error: ${error}`;
return `${error}`; // Just return the error message directly
}
};
19 changes: 11 additions & 8 deletions src/services/webServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type HeadersType = {
type RequestOptions = {
method: string;
headers: Record<string, string>;
// Optional because not all requests will have a body
body?: string;
body?: string; // Optional body, because not all requests will have one
};

export const WebService = {
Expand All @@ -41,20 +40,24 @@ export const WebService = {

try {
const response = await fetch(`${PLAYGROUND_MONO_URL}${url}`, options);
if (!response) {
throw new Error('No response received from the server.');
}

// If the response is not OK, check if the response contains error information
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
const errorResponse = await response.json();
if (errorResponse?.error) {
throw errorResponse.error;
} else {
throw new Error(`HTTP error! Status: ${response.status}`);
}
}

// Parse the result as JSON
const result = await response.json();

return result;
} catch (error) {
if (error instanceof Error)
if (error instanceof Error) {
console.error(`Error with ${method} request: ${error.message}`);
}
throw error;
}
},
Expand Down

0 comments on commit 588f5ac

Please sign in to comment.