41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
import axios from "axios";
|
|
import { Toast } from "react-bootstrap";
|
|
|
|
export class HttpError extends Error {
|
|
constructor(message = "") {
|
|
super(message);
|
|
this.name = "HttpError";
|
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
Toast.error(message, { id: "HttpError" });
|
|
}
|
|
}
|
|
|
|
function responseHandler(response) {
|
|
if (response.status === 200 || response.status === 201) {
|
|
const data = response?.data;
|
|
if (!data) {
|
|
throw new HttpError("API Error. No data!");
|
|
}
|
|
return data;
|
|
}
|
|
throw new HttpError(`API Error! Invalid status code ${response.status}!`);
|
|
}
|
|
|
|
function responseErrorHandler(error) {
|
|
if (error === null) {
|
|
throw new Error("Unrecoverable error!! Error is null!");
|
|
}
|
|
Toast.error(error.message, { id: "AxiosError" });
|
|
return Promise.reject(error.message);
|
|
}
|
|
|
|
export const ApiClient = axios.create({
|
|
baseURL: "http://localhost:8081/",
|
|
timeout: "3000",
|
|
headers: {
|
|
Accept: "application/json",
|
|
},
|
|
});
|
|
|
|
ApiClient.interceptors.response.use(responseHandler, responseErrorHandler);
|