Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Catch permission errors #2290

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions editor.planx.uk/src/lib/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
DefaultContext,
from,
InMemoryCache,
Operation,
} from "@apollo/client";
import { onError } from "@apollo/client/link/error";
import { RetryLink } from "@apollo/client/link/retry";
import { logger } from "airbrake";
import { useStore } from "pages/FlowEditor/lib/store";
import { toast } from "react-toastify";

import { getCookie } from "./cookie";
Expand Down Expand Up @@ -45,14 +48,40 @@ const publicHttpLink = createHttpLink({
headers: { "x-hasura-role": "public" },
});

const errorLink = onError(({ graphQLErrors }) => {
const handlePermissionErrors = (message: string, operation: Operation) => {
const permissionErrors = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a number of issues on the Hasura graphql-engine repo requesting better error messages - please see explanation here for context hasura/graphql-engine#6716 (comment)

// Constraints error - user does not have access to this resource
/permission has failed/gi,
// Query or mutation error - user does not have access to this query
/not found in type/gi,
];

const isPermissionError = permissionErrors.some((re) => re.test(message));

if (isPermissionError) {
const user = useStore.getState().getUser();
const team = useStore.getState().teamName;
logger.notify(
`[Permission error]: User ${user?.id} cannot execute ${operation.operationName} for ${team}`,
);

toast.error("Permission error", {
toastId: "permission_error",
hideProgressBar: true,
progress: undefined,
});
}
};

const errorLink = onError(({ graphQLErrors, operation }) => {
if (graphQLErrors) {
// GraphQL errors are not retried
graphQLErrors.map(({ message, locations, path }) =>
graphQLErrors.forEach(({ message, locations, path }) => {
console.error(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
);
handlePermissionErrors(message, operation);
});
} else {
toast.error("Network error, attempting to reconnect…", {
toastId,
Expand Down
Loading