Skip to content

Commit

Permalink
wip: Basic toast on permission error
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 9, 2023
1 parent 380f27e commit 20d6842
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions editor.planx.uk/src/lib/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ 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 { toast } from "react-toastify";

import { getCookie } from "./cookie";
import { useStore } from "pages/FlowEditor/lib/store";

const toastId = "error_toast";

Expand Down Expand Up @@ -45,14 +48,38 @@ 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
4 changes: 2 additions & 2 deletions editor.planx.uk/src/pages/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ const Team: React.FC = () => {
}}
/>
))}
{useStore.getState().canUserEditTeam(slug) && (
{/* {useStore.getState().canUserEditTeam(slug) && ( */}
<AddButton
onClick={() => {
const newFlowName = prompt("Service name");
Expand All @@ -347,7 +347,7 @@ const Team: React.FC = () => {
>
Add a new service
</AddButton>
)}
{/* )} */}
</DashboardList>
)}
</Dashboard>
Expand Down

0 comments on commit 20d6842

Please sign in to comment.