Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest Client: add json error handling for rest error #127

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/main/webui/src/app/utils/RestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {Utils} from "./AppUtils.js";

const httpCall = (url, method, headers={}, payload) => fetch(url, {
method,
Expand All @@ -35,13 +36,13 @@ const jsonRest ={
put: (url, payload) => httpCall(url, "PUT", {"Content-type": "application/json"}, JSON.stringify(payload))
};

const logErrors = response => {
if(response.text()){
response.text().then(error=>console.log(error));
}else{
console.log(`Something wrong: ${response.status} -> ${response.statusText}`);
}
};
// const logErrors = response => {
// if(response.text()){
// response.text().then(error=>console.log(error));
// }else{
// console.log(`Something wrong: ${response.status} -> ${response.statusText}`);
// }
// };

const BASE_API_PATH = "/api/admin/stores";
const storeAPIEndpoint = (pkgType, type, name) => `${BASE_API_PATH}/${pkgType}/${type}/${name}`;
Expand All @@ -51,9 +52,13 @@ const handleResponse = async response => {
const result = await response.json();
return {result, success: true};
}
const responseMsg = await response.text();
if(responseMsg){
return {success: false, error: {status: response.status, message: responseMsg}};
try {
const responseData = await response.json();
if(responseData){
return {success: false, error: {status: response.status, message: responseData.error}};
}
}catch(e) {
Utils.logMessage(e.type);
}
return {success: false, error: {status: response.status, message: response.statusText}};
};
Expand Down Expand Up @@ -123,4 +128,4 @@ const IndyRest = {



export {http, jsonRest, logErrors, IndyRest, BASE_API_PATH};
export {IndyRest, BASE_API_PATH};
6 changes: 6 additions & 0 deletions src/main/webui/src/app/utils/RestClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ describe('IndyRest test', () => {
expect(result.success).toBe(false);
expect(result.error).toEqual({status: 404, message: "Not Found"});
});
it('Check get store: error with json body', async () => {
fetchMock.mock(`${BASE_API_PATH}/maven/remote/central`, {status: 500, body: JSON.stringify({error: "Mock internal error"})});
const result = await IndyRest.storeRes.get("maven", "remote", "central");
expect(result.success).toBe(false);
expect(result.error).toEqual({status: 500, message: "Mock internal error"});
});
});
Loading