2021-03-22 09:22:24 +00:00
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
ERROR_0,
|
|
|
|
|
ERROR_500,
|
|
|
|
|
SERVER_API_TIMEOUT_ERROR,
|
2022-02-11 18:08:46 +00:00
|
|
|
} from "@appsmith/constants/messages";
|
2021-03-22 09:22:24 +00:00
|
|
|
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
|
|
|
import {
|
|
|
|
|
API_STATUS_CODES,
|
|
|
|
|
ERROR_CODES,
|
|
|
|
|
SERVER_ERROR_CODES,
|
2022-01-07 06:08:17 +00:00
|
|
|
} from "@appsmith/constants/ApiConstants";
|
2021-03-22 09:22:24 +00:00
|
|
|
import log from "loglevel";
|
|
|
|
|
import { ActionExecutionResponse } from "api/ActionAPI";
|
2021-06-16 17:09:25 +00:00
|
|
|
import store from "store";
|
|
|
|
|
import { logoutUser } from "actions/userActions";
|
2021-06-17 17:20:32 +00:00
|
|
|
import { AUTH_LOGIN_URL } from "constants/routes";
|
2021-10-18 14:03:44 +00:00
|
|
|
import { getCurrentGitBranch } from "selectors/gitSyncSelectors";
|
2022-03-31 08:15:23 +00:00
|
|
|
import getQueryParamsObject from "utils/getQueryParamsObject";
|
2022-08-31 18:08:42 +00:00
|
|
|
import { UserCancelledActionExecutionError } from "sagas/ActionExecution/errorUtils";
|
2023-01-06 14:09:38 +00:00
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
|
|
|
import { getAppsmithConfigs } from "ce/configs";
|
2023-02-10 07:56:36 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
|
|
|
|
import { CONTENT_TYPE_HEADER_KEY } from "constants/ApiEditorConstants/CommonApiConstants";
|
2021-03-22 09:22:24 +00:00
|
|
|
|
|
|
|
|
const executeActionRegex = /actions\/execute/;
|
|
|
|
|
const timeoutErrorRegex = /timeout of (\d+)ms exceeded/;
|
|
|
|
|
export const axiosConnectionAbortedCode = "ECONNABORTED";
|
2023-01-06 14:09:38 +00:00
|
|
|
const appsmithConfig = getAppsmithConfigs();
|
2021-03-22 09:22:24 +00:00
|
|
|
|
|
|
|
|
const makeExecuteActionResponse = (response: any): ActionExecutionResponse => ({
|
|
|
|
|
...response.data,
|
|
|
|
|
clientMeta: {
|
|
|
|
|
size: response.headers["content-length"],
|
|
|
|
|
duration: Number(performance.now() - response.config.timer).toFixed(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const is404orAuthPath = () => {
|
|
|
|
|
const pathName = window.location.pathname;
|
|
|
|
|
return /^\/404/.test(pathName) || /^\/user\/\w+/.test(pathName);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Request interceptor will add a timer property to the request.
|
|
|
|
|
// this will be used to calculate the time taken for an action
|
|
|
|
|
// execution request
|
|
|
|
|
export const apiRequestInterceptor = (config: AxiosRequestConfig) => {
|
2023-01-06 14:09:38 +00:00
|
|
|
config.headers = config.headers ?? {};
|
2022-03-31 08:15:23 +00:00
|
|
|
const branch =
|
|
|
|
|
getCurrentGitBranch(store.getState()) || getQueryParamsObject().branch;
|
2022-06-30 11:29:09 +00:00
|
|
|
if (branch && config.headers) {
|
2021-10-26 08:51:26 +00:00
|
|
|
config.headers.branchName = branch;
|
2021-10-18 14:03:44 +00:00
|
|
|
}
|
2022-01-31 04:18:58 +00:00
|
|
|
if (config.url?.indexOf("/git/") !== -1) {
|
|
|
|
|
config.timeout = 1000 * 120; // increase timeout for git specific APIs
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 14:09:38 +00:00
|
|
|
const anonymousId = AnalyticsUtil.getAnonymousId();
|
|
|
|
|
appsmithConfig.segment.enabled &&
|
|
|
|
|
anonymousId &&
|
|
|
|
|
(config.headers["x-anonymous-user-id"] = anonymousId);
|
|
|
|
|
|
2021-03-22 09:22:24 +00:00
|
|
|
return { ...config, timer: performance.now() };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// On success of an API, if the api is an action execution,
|
|
|
|
|
// add the client meta object with size and time taken info
|
|
|
|
|
// otherwise just return the data
|
|
|
|
|
export const apiSuccessResponseInterceptor = (
|
|
|
|
|
response: AxiosResponse,
|
|
|
|
|
): AxiosResponse["data"] => {
|
|
|
|
|
if (response.config.url) {
|
|
|
|
|
if (response.config.url.match(executeActionRegex)) {
|
|
|
|
|
return makeExecuteActionResponse(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-10 07:56:36 +00:00
|
|
|
if (
|
|
|
|
|
response.headers[CONTENT_TYPE_HEADER_KEY] === "application/json" &&
|
|
|
|
|
!response.data.responseMeta
|
|
|
|
|
) {
|
|
|
|
|
Sentry.captureException(new Error("Api responded without response meta"), {
|
|
|
|
|
contexts: { response: response.data },
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-03-22 09:22:24 +00:00
|
|
|
return response.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Handle different api failure scenarios
|
|
|
|
|
export const apiFailureResponseInterceptor = (error: any) => {
|
|
|
|
|
// Return error when there is no internet
|
|
|
|
|
if (!window.navigator.onLine) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
...error,
|
|
|
|
|
message: createMessage(ERROR_0),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return if the call was cancelled via cancel token
|
|
|
|
|
if (axios.isCancel(error)) {
|
2022-08-31 18:08:42 +00:00
|
|
|
throw new UserCancelledActionExecutionError();
|
2021-03-22 09:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return modified response if action execution failed
|
|
|
|
|
if (error.config && error.config.url.match(executeActionRegex)) {
|
|
|
|
|
return makeExecuteActionResponse(error.response);
|
|
|
|
|
}
|
|
|
|
|
// Return error if any timeout happened in other api calls
|
|
|
|
|
if (
|
|
|
|
|
error.code === axiosConnectionAbortedCode &&
|
|
|
|
|
error.message &&
|
|
|
|
|
error.message.match(timeoutErrorRegex)
|
|
|
|
|
) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
...error,
|
|
|
|
|
message: createMessage(SERVER_API_TIMEOUT_ERROR),
|
|
|
|
|
code: ERROR_CODES.REQUEST_TIMEOUT,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error.response) {
|
|
|
|
|
if (error.response.status === API_STATUS_CODES.SERVER_ERROR) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
...error,
|
|
|
|
|
code: ERROR_CODES.SERVER_ERROR,
|
|
|
|
|
message: createMessage(ERROR_500),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
|
if (!is404orAuthPath()) {
|
|
|
|
|
const currentUrl = `${window.location.href}`;
|
|
|
|
|
if (error.response.status === API_STATUS_CODES.REQUEST_NOT_AUTHORISED) {
|
|
|
|
|
// Redirect to login and set a redirect url.
|
2021-06-16 17:09:25 +00:00
|
|
|
store.dispatch(
|
2021-06-17 17:20:32 +00:00
|
|
|
logoutUser({
|
|
|
|
|
redirectURL: `${AUTH_LOGIN_URL}?redirectUrl=${encodeURIComponent(
|
|
|
|
|
currentUrl,
|
|
|
|
|
)}`,
|
|
|
|
|
}),
|
2021-06-16 17:09:25 +00:00
|
|
|
);
|
2021-03-22 09:22:24 +00:00
|
|
|
return Promise.reject({
|
|
|
|
|
code: ERROR_CODES.REQUEST_NOT_AUTHORISED,
|
|
|
|
|
message: "Unauthorized. Redirecting to login page...",
|
|
|
|
|
show: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-02-10 07:56:36 +00:00
|
|
|
const errorData = error.response.data.responseMeta ?? {};
|
2021-03-22 09:22:24 +00:00
|
|
|
if (
|
|
|
|
|
errorData.status === API_STATUS_CODES.RESOURCE_NOT_FOUND &&
|
2021-12-26 14:52:42 +00:00
|
|
|
(errorData.error.code === SERVER_ERROR_CODES.RESOURCE_NOT_FOUND ||
|
|
|
|
|
errorData.error.code === SERVER_ERROR_CODES.UNABLE_TO_FIND_PAGE)
|
2021-03-22 09:22:24 +00:00
|
|
|
) {
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
code: ERROR_CODES.PAGE_NOT_FOUND,
|
|
|
|
|
message: "Resource Not Found",
|
|
|
|
|
show: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (error.response.data.responseMeta) {
|
|
|
|
|
return Promise.resolve(error.response.data);
|
|
|
|
|
}
|
2023-02-10 07:56:36 +00:00
|
|
|
Sentry.captureException(new Error("Api responded without response meta"), {
|
|
|
|
|
contexts: { response: error.response.data },
|
|
|
|
|
});
|
2021-03-22 09:22:24 +00:00
|
|
|
return Promise.reject(error.response.data);
|
|
|
|
|
} else if (error.request) {
|
|
|
|
|
// The request was made but no response was received
|
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
|
// http.ClientRequest in node.js
|
|
|
|
|
log.error(error.request);
|
|
|
|
|
} else {
|
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
|
log.error("Error", error.message);
|
|
|
|
|
}
|
2021-08-20 04:13:16 +00:00
|
|
|
log.debug(error.config);
|
2021-03-22 09:22:24 +00:00
|
|
|
return Promise.resolve(error);
|
|
|
|
|
};
|