|
5 | 5 | * |
6 | 6 | * Copyright Oxide Computer Company |
7 | 7 | */ |
| 8 | +import { useQuery } from '@tanstack/react-query' |
8 | 9 | import { ErrorBoundary as BaseErrorBoundary } from 'react-error-boundary' |
9 | 10 | import { useRouteError } from 'react-router' |
10 | 11 |
|
| 12 | +import { apiq } from '~/api' |
11 | 13 | import { type ApiError } from '~/api/errors' |
| 14 | +import { Message } from '~/ui/lib/Message' |
| 15 | +import { links } from '~/util/links' |
12 | 16 |
|
13 | 17 | import { ErrorPage, NotFound } from './ErrorPage' |
14 | 18 |
|
| 19 | +const IdpMisconfig = () => ( |
| 20 | + <Message |
| 21 | + variant="notice" |
| 22 | + className="!mt-6" |
| 23 | + showIcon={false} |
| 24 | + content={ |
| 25 | + <p> |
| 26 | + You are not in any groups and have no role on the silo. This usually means the |
| 27 | + identity provider is not set up correctly. Read the{' '} |
| 28 | + <a |
| 29 | + href={links.troubleshootingAccess} |
| 30 | + className="underline" |
| 31 | + target="_blank" |
| 32 | + rel="noreferrer" |
| 33 | + > |
| 34 | + docs |
| 35 | + </a>{' '} |
| 36 | + for more information. |
| 37 | + </p> |
| 38 | + } |
| 39 | + /> |
| 40 | +) |
| 41 | + |
| 42 | +function useDetectNoSiloRole(enabled: boolean): boolean { |
| 43 | + // this is kind of a hail mary, so if any of this goes wrong we need to ignore it |
| 44 | + const options = { enabled, throwOnError: false } |
| 45 | + const { data: me } = useQuery(apiq('currentUserView', {}, options)) |
| 46 | + const { data: myGroups } = useQuery(apiq('currentUserGroups', {}, options)) |
| 47 | + const { data: siloPolicy } = useQuery(apiq('policyView', {}, options)) |
| 48 | + |
| 49 | + if (!me || !myGroups || !siloPolicy) return false |
| 50 | + |
| 51 | + const noGroups = myGroups.items.length === 0 |
| 52 | + const hasDirectRoleOnSilo = siloPolicy.roleAssignments.some((r) => r.identityId === me.id) |
| 53 | + return noGroups && !hasDirectRoleOnSilo |
| 54 | +} |
| 55 | + |
15 | 56 | export const trigger404 = { type: 'error', statusCode: 404 } |
16 | 57 |
|
17 | 58 | type Props = { error: Error | ApiError } |
18 | 59 |
|
19 | 60 | function ErrorFallback({ error }: Props) { |
20 | 61 | console.error(error) |
| 62 | + const statusCode = 'statusCode' in error ? error.statusCode : undefined |
| 63 | + |
| 64 | + // if the error is a 403, make API calls to check whether the user has any |
| 65 | + // groups or any roles directly on the silo |
| 66 | + const showIdpMisconfig = useDetectNoSiloRole(statusCode === 403) |
21 | 67 |
|
22 | | - if ('statusCode' in error && error.statusCode === 404) { |
23 | | - return <NotFound /> |
24 | | - } |
| 68 | + if (statusCode === 404) return <NotFound /> |
25 | 69 |
|
26 | 70 | return ( |
27 | 71 | <ErrorPage> |
28 | 72 | <h1 className="text-sans-2xl">Something went wrong</h1> |
29 | 73 | <p className="text-secondary"> |
30 | 74 | Please try again. If the problem persists, contact your administrator. |
31 | 75 | </p> |
| 76 | + {showIdpMisconfig && <IdpMisconfig />} |
32 | 77 | </ErrorPage> |
33 | 78 | ) |
34 | 79 | } |
|
0 commit comments