2022-02-21 13:32:38 +00:00
|
|
|
import { ApolloError } from "@apollo/client";
|
2020-07-24 09:17:25 +00:00
|
|
|
import { IMessageContext } from "@saleor/components/messages";
|
2020-05-25 23:38:52 +00:00
|
|
|
import { UseNotifierResult } from "@saleor/hooks/useNotifier";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
|
|
|
import { IntlShape } from "react-intl";
|
|
|
|
|
2020-07-27 09:39:00 +00:00
|
|
|
import { isJwtError, isTokenExpired } from "./errors";
|
2020-07-24 09:17:25 +00:00
|
|
|
|
2020-05-25 23:38:52 +00:00
|
|
|
export const displayDemoMessage = (
|
|
|
|
intl: IntlShape,
|
|
|
|
notify: UseNotifierResult
|
|
|
|
) => {
|
|
|
|
notify({
|
|
|
|
text: intl.formatMessage(commonMessages.demo)
|
|
|
|
});
|
|
|
|
};
|
2020-07-24 09:17:25 +00:00
|
|
|
|
|
|
|
export async function handleQueryAuthError(
|
|
|
|
error: ApolloError,
|
|
|
|
notify: IMessageContext,
|
|
|
|
logout: () => void,
|
|
|
|
intl: IntlShape
|
|
|
|
) {
|
|
|
|
if (error.graphQLErrors.some(isJwtError)) {
|
2021-12-17 11:10:54 +00:00
|
|
|
logout();
|
2020-07-27 09:39:00 +00:00
|
|
|
if (error.graphQLErrors.every(isTokenExpired)) {
|
2021-12-17 11:10:54 +00:00
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.sessionExpired)
|
|
|
|
});
|
2020-07-24 09:17:25 +00:00
|
|
|
} else {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.somethingWentWrong)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
!error.graphQLErrors.every(
|
|
|
|
err => err.extensions?.exception?.code === "PermissionDenied"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.somethingWentWrong)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|