Skip to content

Commit

Permalink
feat: Catch permission errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 9, 2023
1 parent 380f27e commit 9f57d40
Showing 1 changed file with 33 additions and 4 deletions.
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 = [
// 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

0 comments on commit 9f57d40

Please sign in to comment.