2021-01-06 11:24:16 +00:00
|
|
|
import { get } from "lodash";
|
2025-01-10 04:51:54 +00:00
|
|
|
import { type ReduxAction } from "actions/ReduxActionTypes";
|
2024-09-05 05:36:43 +00:00
|
|
|
import {
|
2019-09-27 16:05:33 +00:00
|
|
|
ReduxActionErrorTypes,
|
2025-02-06 06:41:53 +00:00
|
|
|
ReduxActionTypes,
|
|
|
|
|
toastMessageErrorTypes,
|
2024-08-06 14:52:22 +00:00
|
|
|
} from "ee/constants/ReduxActionConstants";
|
2021-01-06 11:24:16 +00:00
|
|
|
import log from "loglevel";
|
|
|
|
|
import history from "utils/history";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { ApiResponse } from "api/ApiResponses";
|
2025-02-06 06:41:53 +00:00
|
|
|
import {
|
|
|
|
|
type ErrorPayloadType,
|
|
|
|
|
flushErrors,
|
|
|
|
|
safeCrashApp,
|
|
|
|
|
} from "actions/errorActions";
|
2021-01-06 11:24:16 +00:00
|
|
|
import { AUTH_LOGIN_URL } from "constants/routes";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { User } from "constants/userConstants";
|
2025-02-06 06:41:53 +00:00
|
|
|
import { ANONYMOUS_USERNAME } from "constants/userConstants";
|
|
|
|
|
import {
|
|
|
|
|
AXIOS_CONNECTION_ABORTED_CODE,
|
|
|
|
|
ERROR_CODES,
|
|
|
|
|
SERVER_ERROR_CODES,
|
|
|
|
|
} from "ee/constants/ApiConstants";
|
2020-12-17 07:03:59 +00:00
|
|
|
import { getSafeCrash } from "selectors/errorSelectors";
|
2021-01-06 11:24:16 +00:00
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
2025-02-06 06:41:53 +00:00
|
|
|
import { call, put, select, takeLatest } from "redux-saga/effects";
|
2021-01-21 06:42:53 +00:00
|
|
|
import {
|
2025-02-06 06:41:53 +00:00
|
|
|
createMessage,
|
|
|
|
|
DEFAULT_ERROR_MESSAGE,
|
|
|
|
|
ERROR_0,
|
2021-01-21 06:42:53 +00:00
|
|
|
ERROR_401,
|
2022-12-01 06:30:50 +00:00
|
|
|
ERROR_403,
|
2021-01-21 06:42:53 +00:00
|
|
|
ERROR_500,
|
2024-08-06 14:52:22 +00:00
|
|
|
} from "ee/constants/messages";
|
2022-12-01 06:30:50 +00:00
|
|
|
import store from "store";
|
2019-09-27 16:05:33 +00:00
|
|
|
|
2021-06-23 15:42:07 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2024-08-06 14:52:22 +00:00
|
|
|
import { getLoginUrl } from "ee/utils/adminSettingsHelpers";
|
2023-04-03 04:26:18 +00:00
|
|
|
import type { PluginErrorDetails } from "api/ActionAPI";
|
2023-06-09 13:58:45 +00:00
|
|
|
import showToast from "sagas/ToastSagas";
|
2024-09-05 05:36:43 +00:00
|
|
|
import AppsmithConsole from "../utils/AppsmithConsole";
|
|
|
|
|
import type { SourceEntity } from "../entities/AppsmithConsole";
|
|
|
|
|
import { getAppMode } from "ee/selectors/applicationSelectors";
|
|
|
|
|
import { APP_MODE } from "../entities/App";
|
|
|
|
|
|
|
|
|
|
const shouldShowToast = (action: string) => {
|
|
|
|
|
return action in toastMessageErrorTypes;
|
|
|
|
|
};
|
2021-06-23 15:42:07 +00:00
|
|
|
|
2021-01-21 11:08:18 +00:00
|
|
|
/**
|
|
|
|
|
* making with error message with action name
|
|
|
|
|
*
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
export const getDefaultActionError = (action: string) =>
|
|
|
|
|
`Incurred an error when ${action}`;
|
|
|
|
|
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-12-16 08:49:10 +00:00
|
|
|
export function* callAPI(apiCall: any, requestPayload: any) {
|
|
|
|
|
try {
|
2022-06-21 13:57:34 +00:00
|
|
|
const response: ApiResponse = yield call(apiCall, requestPayload);
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2022-06-21 13:57:34 +00:00
|
|
|
return response;
|
2019-12-16 08:49:10 +00:00
|
|
|
} catch (error) {
|
2022-06-21 13:57:34 +00:00
|
|
|
return error;
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-21 11:08:18 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-02-21 13:38:16 +00:00
|
|
|
* transform server errors to client error codes
|
2021-01-21 11:08:18 +00:00
|
|
|
*
|
|
|
|
|
* @param code
|
2023-02-21 13:38:16 +00:00
|
|
|
* @param resourceType
|
2021-01-21 11:08:18 +00:00
|
|
|
*/
|
2022-12-01 06:30:50 +00:00
|
|
|
const getErrorMessage = (code: number, resourceType = "") => {
|
2019-12-16 08:49:10 +00:00
|
|
|
switch (code) {
|
2020-04-08 08:43:56 +00:00
|
|
|
case 401:
|
2021-03-13 14:24:45 +00:00
|
|
|
return createMessage(ERROR_401);
|
2019-12-16 08:49:10 +00:00
|
|
|
case 500:
|
2021-03-13 14:24:45 +00:00
|
|
|
return createMessage(ERROR_500);
|
2022-12-01 06:30:50 +00:00
|
|
|
case 403:
|
|
|
|
|
return createMessage(() =>
|
|
|
|
|
ERROR_403(resourceType, getCurrentUser(store.getState())?.email || ""),
|
|
|
|
|
);
|
2020-04-05 06:34:14 +00:00
|
|
|
case 0:
|
2021-03-13 14:24:45 +00:00
|
|
|
return createMessage(ERROR_0);
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-04 05:24:47 +00:00
|
|
|
export class IncorrectBindingError extends Error {}
|
|
|
|
|
|
2021-01-21 11:08:18 +00:00
|
|
|
/**
|
|
|
|
|
* validates if response does have any errors
|
2022-07-11 04:06:29 +00:00
|
|
|
* @throws {Error}
|
2021-01-21 11:08:18 +00:00
|
|
|
* @param response
|
|
|
|
|
* @param show
|
2023-02-21 13:38:16 +00:00
|
|
|
* @param logToSentry
|
2021-01-21 11:08:18 +00:00
|
|
|
*/
|
2021-12-21 19:56:32 +00:00
|
|
|
export function* validateResponse(
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-12-21 19:56:32 +00:00
|
|
|
response: ApiResponse | any,
|
|
|
|
|
show = true,
|
|
|
|
|
logToSentry = false,
|
|
|
|
|
) {
|
2020-04-08 08:43:56 +00:00
|
|
|
if (!response) {
|
|
|
|
|
throw Error("");
|
|
|
|
|
}
|
2021-10-14 13:08:24 +00:00
|
|
|
|
|
|
|
|
// letting `apiFailureResponseInterceptor` handle it this case
|
2024-09-25 10:59:21 +00:00
|
|
|
if (response?.code === AXIOS_CONNECTION_ABORTED_CODE) {
|
2021-10-14 13:08:24 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 06:34:14 +00:00
|
|
|
if (!response.responseMeta && !response.status) {
|
|
|
|
|
throw Error(getErrorMessage(0));
|
|
|
|
|
}
|
2023-04-07 13:51:35 +00:00
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
if (!response.responseMeta && response.status) {
|
2024-09-11 08:36:28 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.API_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error: new Error(
|
|
|
|
|
getErrorMessage(response.status, response.resourceType),
|
|
|
|
|
),
|
|
|
|
|
logToSentry,
|
|
|
|
|
show,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
2023-04-07 13:51:35 +00:00
|
|
|
|
2019-09-27 16:05:33 +00:00
|
|
|
if (response.responseMeta.success) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-04-07 13:51:35 +00:00
|
|
|
|
2021-09-01 03:40:44 +00:00
|
|
|
if (
|
2023-02-18 12:55:46 +00:00
|
|
|
SERVER_ERROR_CODES.INCORRECT_BINDING_LIST_OF_WIDGET.includes(
|
|
|
|
|
response.responseMeta.error.code,
|
|
|
|
|
)
|
2021-09-01 03:40:44 +00:00
|
|
|
) {
|
|
|
|
|
throw new IncorrectBindingError(response.responseMeta.error.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.API_ERROR,
|
|
|
|
|
payload: {
|
2023-07-25 05:03:52 +00:00
|
|
|
error: new Error(response.responseMeta.error.message),
|
2021-12-21 19:56:32 +00:00
|
|
|
logToSentry,
|
2021-09-01 03:40:44 +00:00
|
|
|
show,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
throw Error(response.responseMeta.error.message);
|
2019-09-27 16:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
export function getResponseErrorMessage(response: ApiResponse) {
|
|
|
|
|
return response.responseMeta.error
|
|
|
|
|
? response.responseMeta.error.message
|
|
|
|
|
: undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 07:35:24 +00:00
|
|
|
interface ClientDefinedErrorMetadata {
|
2023-04-03 04:26:18 +00:00
|
|
|
clientDefinedError: boolean;
|
|
|
|
|
statusCode: string;
|
|
|
|
|
message: string;
|
|
|
|
|
pluginErrorDetails: PluginErrorDetails;
|
2023-10-11 07:35:24 +00:00
|
|
|
}
|
2023-04-03 04:26:18 +00:00
|
|
|
|
|
|
|
|
export function extractClientDefinedErrorMetadata(
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-04-03 04:26:18 +00:00
|
|
|
err: any,
|
|
|
|
|
): ClientDefinedErrorMetadata | undefined {
|
|
|
|
|
if (err?.clientDefinedError && err?.response) {
|
|
|
|
|
return {
|
|
|
|
|
clientDefinedError: err?.clientDefinedError,
|
|
|
|
|
statusCode: err?.statusCode,
|
|
|
|
|
message: err?.message,
|
|
|
|
|
pluginErrorDetails: err?.pluginErrorDetails,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 06:42:53 +00:00
|
|
|
const ActionErrorDisplayMap: {
|
2019-10-07 13:19:29 +00:00
|
|
|
[key: string]: (error: ErrorPayloadType) => string;
|
2021-01-21 06:42:53 +00:00
|
|
|
} = {
|
2020-12-24 04:32:25 +00:00
|
|
|
[ReduxActionErrorTypes.API_ERROR]: (error) =>
|
2021-03-13 14:24:45 +00:00
|
|
|
get(error, "message", createMessage(DEFAULT_ERROR_MESSAGE)),
|
2019-10-07 13:11:18 +00:00
|
|
|
[ReduxActionErrorTypes.FETCH_PAGE_ERROR]: () =>
|
2021-01-21 11:08:18 +00:00
|
|
|
getDefaultActionError("fetching the page"),
|
2019-10-07 13:11:18 +00:00
|
|
|
[ReduxActionErrorTypes.SAVE_PAGE_ERROR]: () =>
|
2021-01-21 11:08:18 +00:00
|
|
|
getDefaultActionError("saving the page"),
|
2019-10-07 13:11:18 +00:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 06:42:53 +00:00
|
|
|
const getErrorMessageFromActionType = (
|
|
|
|
|
type: string,
|
|
|
|
|
error: ErrorPayloadType,
|
|
|
|
|
): string => {
|
|
|
|
|
const actionErrorMessage = get(error, "message");
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2021-01-21 06:42:53 +00:00
|
|
|
if (actionErrorMessage === undefined) {
|
|
|
|
|
if (type in ActionErrorDisplayMap) {
|
|
|
|
|
return ActionErrorDisplayMap[type](error);
|
|
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2021-03-13 14:24:45 +00:00
|
|
|
return createMessage(DEFAULT_ERROR_MESSAGE);
|
2021-01-21 06:42:53 +00:00
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2021-01-21 06:42:53 +00:00
|
|
|
return actionErrorMessage;
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
enum ErrorEffectTypes {
|
|
|
|
|
SHOW_ALERT = "SHOW_ALERT",
|
|
|
|
|
SAFE_CRASH = "SAFE_CRASH",
|
2024-09-05 05:36:43 +00:00
|
|
|
LOG_TO_CONSOLE = "LOG_TO_CONSOLE",
|
2021-06-23 15:42:07 +00:00
|
|
|
LOG_TO_SENTRY = "LOG_TO_SENTRY",
|
2024-09-05 05:36:43 +00:00
|
|
|
LOG_TO_DEBUGGER = "LOG_TO_DEBUGGER",
|
2020-12-08 19:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 09:38:25 +00:00
|
|
|
export interface ErrorActionPayload {
|
|
|
|
|
error: ErrorPayloadType;
|
|
|
|
|
show?: boolean;
|
|
|
|
|
crash?: boolean;
|
2021-06-23 15:42:07 +00:00
|
|
|
logToSentry?: boolean;
|
2024-09-05 05:36:43 +00:00
|
|
|
logToDebugger?: boolean;
|
|
|
|
|
sourceEntity?: SourceEntity;
|
2021-02-04 09:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* errorSaga(errorAction: ReduxAction<ErrorActionPayload>) {
|
2024-09-05 05:36:43 +00:00
|
|
|
const effects = [ErrorEffectTypes.LOG_TO_CONSOLE];
|
2021-05-13 08:35:39 +00:00
|
|
|
const { payload, type } = errorAction;
|
2024-09-11 08:36:28 +00:00
|
|
|
const { error, logToDebugger, logToSentry, show, sourceEntity } =
|
|
|
|
|
payload || {};
|
2024-09-05 05:36:43 +00:00
|
|
|
const appMode: APP_MODE = yield select(getAppMode);
|
2020-07-15 07:19:52 +00:00
|
|
|
|
2024-09-05 05:36:43 +00:00
|
|
|
// "show" means show a toast. We check if the error has been asked to not been shown
|
2024-09-11 08:36:28 +00:00
|
|
|
// By checking undefined, undecided actions still pass through this check
|
|
|
|
|
if (show === undefined) {
|
2024-09-05 05:36:43 +00:00
|
|
|
// We want to show toasts for certain actions only so we avoid issues or if it is outside edit mode
|
|
|
|
|
if (shouldShowToast(type) || appMode !== APP_MODE.EDIT) {
|
|
|
|
|
effects.push(ErrorEffectTypes.SHOW_ALERT);
|
|
|
|
|
}
|
2024-09-11 08:36:28 +00:00
|
|
|
// If true is passed, show the error no matter what
|
|
|
|
|
} else if (show) {
|
|
|
|
|
effects.push(ErrorEffectTypes.SHOW_ALERT);
|
2024-09-05 05:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (logToDebugger) {
|
|
|
|
|
effects.push(ErrorEffectTypes.LOG_TO_DEBUGGER);
|
2020-12-08 19:13:48 +00:00
|
|
|
}
|
2021-01-11 05:28:10 +00:00
|
|
|
|
2020-12-09 12:52:36 +00:00
|
|
|
if (error && error.crash) {
|
2023-03-30 11:20:26 +00:00
|
|
|
effects.push(ErrorEffectTypes.LOG_TO_SENTRY);
|
2020-12-08 19:13:48 +00:00
|
|
|
effects.push(ErrorEffectTypes.SAFE_CRASH);
|
|
|
|
|
}
|
2021-01-06 11:24:16 +00:00
|
|
|
|
2021-06-23 15:42:07 +00:00
|
|
|
if (error && logToSentry) {
|
|
|
|
|
effects.push(ErrorEffectTypes.LOG_TO_SENTRY);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 05:36:43 +00:00
|
|
|
const message = getErrorMessageFromActionType(type, error);
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
for (const effect of effects) {
|
|
|
|
|
switch (effect) {
|
2024-09-05 05:36:43 +00:00
|
|
|
case ErrorEffectTypes.LOG_TO_CONSOLE: {
|
2020-12-08 19:13:48 +00:00
|
|
|
logErrorSaga(errorAction);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-09-05 05:36:43 +00:00
|
|
|
case ErrorEffectTypes.LOG_TO_DEBUGGER: {
|
|
|
|
|
AppsmithConsole.error({
|
|
|
|
|
text: message,
|
|
|
|
|
source: sourceEntity,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-08 19:13:48 +00:00
|
|
|
case ErrorEffectTypes.SHOW_ALERT: {
|
2023-06-09 13:58:45 +00:00
|
|
|
// This is the toast that is rendered when any page load API fails.
|
|
|
|
|
yield call(showToast, message, { kind: "error" });
|
2023-07-10 05:53:45 +00:00
|
|
|
|
2024-09-05 05:36:43 +00:00
|
|
|
if ("Cypress" in window) {
|
2023-07-10 05:53:45 +00:00
|
|
|
if (message === "" || message === null) {
|
|
|
|
|
yield put(
|
|
|
|
|
safeCrashApp({
|
|
|
|
|
...error,
|
|
|
|
|
code: ERROR_CODES.CYPRESS_DEBUG,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ErrorEffectTypes.SAFE_CRASH: {
|
2023-04-05 08:39:00 +00:00
|
|
|
yield put(safeCrashApp(error));
|
2020-12-08 19:13:48 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2021-06-23 15:42:07 +00:00
|
|
|
case ErrorEffectTypes.LOG_TO_SENTRY: {
|
|
|
|
|
yield call(Sentry.captureException, error);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-08 19:13:48 +00:00
|
|
|
}
|
2020-04-15 14:19:39 +00:00
|
|
|
}
|
2021-01-06 11:24:16 +00:00
|
|
|
|
2019-09-27 16:05:33 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.REPORT_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
source: errorAction.type,
|
2020-12-08 19:13:48 +00:00
|
|
|
message,
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-07-10 05:53:45 +00:00
|
|
|
stackTrace: (error as any)?.stack,
|
2019-09-27 16:05:33 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
function logErrorSaga(action: ReduxAction<{ error: ErrorPayloadType }>) {
|
|
|
|
|
log.debug(`Error in action ${action.type}`);
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2023-07-03 13:06:05 +00:00
|
|
|
if (action.payload) log.error(action.payload.error, action);
|
2020-12-08 19:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 06:44:16 +00:00
|
|
|
export function embedRedirectURL() {
|
|
|
|
|
const queryParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const ssoTriggerQueryParam = queryParams.get("ssoTrigger");
|
|
|
|
|
const ssoLoginUrl = ssoTriggerQueryParam
|
|
|
|
|
? getLoginUrl(ssoTriggerQueryParam || "")
|
|
|
|
|
: null;
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2024-01-24 06:44:16 +00:00
|
|
|
if (ssoLoginUrl) {
|
|
|
|
|
window.location.href = `${ssoLoginUrl}?redirectUrl=${encodeURIComponent(
|
|
|
|
|
window.location.href,
|
|
|
|
|
)}`;
|
|
|
|
|
} else {
|
|
|
|
|
window.location.href = `${AUTH_LOGIN_URL}?redirectUrl=${encodeURIComponent(
|
|
|
|
|
window.location.href,
|
|
|
|
|
)}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2021-01-06 11:24:16 +00:00
|
|
|
/**
|
|
|
|
|
* this saga do some logic before actually setting safeCrash to true
|
|
|
|
|
*/
|
2023-04-05 08:39:00 +00:00
|
|
|
function* safeCrashSagaRequest(action: ReduxAction<{ code?: ERROR_CODES }>) {
|
2023-08-11 11:54:26 +00:00
|
|
|
const user: User | undefined = yield select(getCurrentUser);
|
2021-01-06 11:24:16 +00:00
|
|
|
const code = get(action, "payload.code");
|
|
|
|
|
|
|
|
|
|
// if user is not logged and the error is "PAGE_NOT_FOUND",
|
2023-08-11 11:54:26 +00:00
|
|
|
// redirecting user to login page with redirecTo param
|
2021-01-06 11:24:16 +00:00
|
|
|
if (
|
2021-01-11 05:28:10 +00:00
|
|
|
get(user, "email") === ANONYMOUS_USERNAME &&
|
2021-01-06 11:24:16 +00:00
|
|
|
code === ERROR_CODES.PAGE_NOT_FOUND
|
|
|
|
|
) {
|
2024-01-24 06:44:16 +00:00
|
|
|
embedRedirectURL();
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2021-01-06 11:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if there is no action to be done, just calling the safe crash action
|
2023-04-05 08:39:00 +00:00
|
|
|
yield put(safeCrashApp({ code }));
|
2021-01-06 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-17 07:03:59 +00:00
|
|
|
/**
|
|
|
|
|
* flush errors and redirect users to a url
|
|
|
|
|
*
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
export function* flushErrorsAndRedirectSaga(
|
|
|
|
|
action: ReduxAction<{ url?: string }>,
|
|
|
|
|
) {
|
2022-06-21 13:57:34 +00:00
|
|
|
const safeCrash: boolean = yield select(getSafeCrash);
|
2020-12-17 07:03:59 +00:00
|
|
|
|
|
|
|
|
if (safeCrash) {
|
|
|
|
|
yield put(flushErrors());
|
|
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2022-12-15 04:06:13 +00:00
|
|
|
if (!action.payload.url) return;
|
2020-12-17 07:03:59 +00:00
|
|
|
|
|
|
|
|
history.push(action.payload.url);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 16:05:33 +00:00
|
|
|
export default function* errorSagas() {
|
|
|
|
|
yield takeLatest(Object.values(ReduxActionErrorTypes), errorSaga);
|
2020-12-17 07:03:59 +00:00
|
|
|
yield takeLatest(
|
|
|
|
|
ReduxActionTypes.FLUSH_AND_REDIRECT,
|
|
|
|
|
flushErrorsAndRedirectSaga,
|
|
|
|
|
);
|
2021-01-06 11:24:16 +00:00
|
|
|
yield takeLatest(
|
|
|
|
|
ReduxActionTypes.SAFE_CRASH_APPSMITH_REQUEST,
|
|
|
|
|
safeCrashSagaRequest,
|
|
|
|
|
);
|
2019-09-27 16:05:33 +00:00
|
|
|
}
|