Skip to content

Commit

Permalink
Merge pull request #127 from ligangty/rest
Browse files Browse the repository at this point in the history
Rest Client: add json error handling for rest error
  • Loading branch information
ligangty authored Dec 20, 2023
2 parents 6fbcaa6 + eeed951 commit a02601e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
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"});
});
});

0 comments on commit a02601e

Please sign in to comment.