2022-03-04 10:52:58 +00:00
|
|
|
import { ApolloError } from "@apollo/client/core";
|
|
|
|
import { IMessage, IMessageContext } from "@saleor/components/messages";
|
2020-05-25 23:38:52 +00:00
|
|
|
import { UseNotifierResult } from "@saleor/hooks/useNotifier";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2022-03-04 10:52:58 +00:00
|
|
|
import { getMutationErrors, parseLogMessage } from "@saleor/misc";
|
|
|
|
import { ServerErrorWithName } from "@saleor/types";
|
2020-05-25 23:38:52 +00:00
|
|
|
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
|
|
|
|
2022-03-04 10:52:58 +00:00
|
|
|
const getAllErrorMessages = (error: ApolloError) => {
|
|
|
|
const errorMessages = [];
|
|
|
|
|
|
|
|
if (error.graphQLErrors.length) {
|
|
|
|
error.graphQLErrors.forEach(err => {
|
|
|
|
errorMessages.push(err.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const networkErrors = error.networkError as ServerErrorWithName;
|
|
|
|
|
|
|
|
if (error.networkError) {
|
|
|
|
// Apparently network errors can be an object or an array
|
|
|
|
if (Array.isArray(networkErrors.result)) {
|
|
|
|
networkErrors.result.forEach(result => {
|
|
|
|
if (result.errors) {
|
|
|
|
result.errors.forEach(({ message }) => {
|
|
|
|
errorMessages.push(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
errorMessages.push(networkErrors.result.errors.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorMessages;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const showAllErrors = ({
|
|
|
|
notify,
|
|
|
|
error
|
|
|
|
}: {
|
|
|
|
notify: IMessageContext;
|
|
|
|
error: ApolloError;
|
|
|
|
}) => {
|
|
|
|
getAllErrorMessages(error).forEach(message => {
|
|
|
|
notify({
|
|
|
|
text: error.message,
|
|
|
|
status: "error",
|
|
|
|
apiMessage: message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const handleNestedMutationErrors = ({
|
|
|
|
data,
|
|
|
|
intl,
|
|
|
|
notify
|
|
|
|
}: {
|
|
|
|
data: any;
|
|
|
|
intl: IntlShape;
|
|
|
|
notify: (message: IMessage) => void;
|
|
|
|
}) => {
|
|
|
|
const mutationErrors = getMutationErrors({ data });
|
|
|
|
|
|
|
|
if (mutationErrors.length > 0) {
|
|
|
|
mutationErrors.forEach(error => {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: error.message,
|
|
|
|
apiMessage: parseLogMessage({
|
|
|
|
intl,
|
|
|
|
code: error.code,
|
|
|
|
field: error.field
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 {
|
2022-03-04 10:52:58 +00:00
|
|
|
showAllErrors({ notify, error });
|
2020-07-24 09:17:25 +00:00
|
|
|
}
|
2022-03-04 10:52:58 +00:00
|
|
|
} else {
|
|
|
|
showAllErrors({ notify, error });
|
2020-07-24 09:17:25 +00:00
|
|
|
}
|
|
|
|
}
|