| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {TrackErrorName} from 'isl-server/src/analytics/eventNames'; |
| b69ab31 | | | 9 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 10 | import type {MessageBusStatus} from './MessageBus'; |
| b69ab31 | | | 11 | import type {RepoInfo} from './types'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 14 | import {ErrorNotice} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 15 | import {useAtomValue} from 'jotai'; |
| b69ab31 | | | 16 | import {useThrottledEffect} from 'shared/hooks'; |
| b69ab31 | | | 17 | import {Internal} from './Internal'; |
| b69ab31 | | | 18 | import {tracker} from './analytics'; |
| b69ab31 | | | 19 | import {allDiffSummaries} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 20 | import {t, T} from './i18n'; |
| b69ab31 | | | 21 | import platform from './platform'; |
| b69ab31 | | | 22 | import {reconnectingStatus, repositoryInfoOrError} from './serverAPIState'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | type TopLevelErrorInfo = { |
| b69ab31 | | | 25 | title: ReactNode; |
| b69ab31 | | | 26 | error: Error; |
| b69ab31 | | | 27 | buttons?: Array<ReactNode>; |
| b69ab31 | | | 28 | trackErrorName?: TrackErrorName; |
| b69ab31 | | | 29 | }; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | function computeTopLevelError( |
| b69ab31 | | | 32 | repoInfo: RepoInfo | undefined, |
| b69ab31 | | | 33 | reconnectStatus: MessageBusStatus, |
| b69ab31 | | | 34 | diffFetchError: Error | undefined, |
| b69ab31 | | | 35 | ): TopLevelErrorInfo | undefined { |
| b69ab31 | | | 36 | if (reconnectStatus.type === 'reconnecting') { |
| b69ab31 | | | 37 | return { |
| b69ab31 | | | 38 | title: <T>Connection to server was lost</T>, |
| b69ab31 | | | 39 | error: new Error(t('Attempting to reconnect...')), |
| b69ab31 | | | 40 | }; |
| b69ab31 | | | 41 | } else if (reconnectStatus.type === 'error') { |
| b69ab31 | | | 42 | if (reconnectStatus.error === 'Invalid token') { |
| b69ab31 | | | 43 | return { |
| b69ab31 | | | 44 | title: ( |
| b69ab31 | | | 45 | <T> |
| b69ab31 | | | 46 | Unable to connect to server. Try closing this window and accessing ISL with a fresh |
| b69ab31 | | | 47 | link. |
| b69ab31 | | | 48 | </T> |
| b69ab31 | | | 49 | ), |
| b69ab31 | | | 50 | error: new Error( |
| b69ab31 | | | 51 | t( |
| b69ab31 | | | 52 | 'Invalid connection token. ' + |
| b69ab31 | | | 53 | 'For security, you need to open a new ISL window when the server is restarted.', |
| b69ab31 | | | 54 | ), |
| b69ab31 | | | 55 | ), |
| b69ab31 | | | 56 | }; |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | return { |
| b69ab31 | | | 59 | title: <T>Error connecting to server</T>, |
| b69ab31 | | | 60 | error: new Error(reconnectStatus.error), |
| b69ab31 | | | 61 | // no use tracking, since we can't reach the server anyway. |
| b69ab31 | | | 62 | }; |
| b69ab31 | | | 63 | } else if (diffFetchError) { |
| b69ab31 | | | 64 | const internalResult = Internal.findInternalError?.(diffFetchError) as |
| b69ab31 | | | 65 | | TopLevelErrorInfo |
| b69ab31 | | | 66 | | undefined; |
| b69ab31 | | | 67 | if (internalResult != null) { |
| b69ab31 | | | 68 | return internalResult; |
| b69ab31 | | | 69 | } else if (repoInfo?.type === 'success' && repoInfo.codeReviewSystem.type === 'github') { |
| b69ab31 | | | 70 | const learnAboutGhButton = ( |
| b69ab31 | | | 71 | <Button |
| b69ab31 | | | 72 | onClick={e => { |
| b69ab31 | | | 73 | platform.openExternalLink('https://sapling-scm.com/docs/git/intro'); |
| b69ab31 | | | 74 | e.preventDefault(); |
| b69ab31 | | | 75 | e.stopPropagation(); |
| b69ab31 | | | 76 | }}> |
| b69ab31 | | | 77 | <T>Learn more</T> |
| b69ab31 | | | 78 | </Button> |
| b69ab31 | | | 79 | ); |
| b69ab31 | | | 80 | if (diffFetchError.message.startsWith('NotAuthenticatedError')) { |
| b69ab31 | | | 81 | const error = new Error( |
| b69ab31 | | | 82 | t('Log in to gh CLI with `gh auth login` to allow requests to GitHub'), |
| b69ab31 | | | 83 | ); |
| b69ab31 | | | 84 | error.stack = diffFetchError.stack; |
| b69ab31 | | | 85 | return { |
| b69ab31 | | | 86 | title: <T>Not Authenticated to GitHub with `gh` CLI</T>, |
| b69ab31 | | | 87 | error, |
| b69ab31 | | | 88 | buttons: [learnAboutGhButton], |
| b69ab31 | | | 89 | trackErrorName: 'GhCliNotAuthenticated', |
| b69ab31 | | | 90 | }; |
| b69ab31 | | | 91 | } else if (diffFetchError.message.startsWith('GhNotInstalledError')) { |
| b69ab31 | | | 92 | const error = new Error(t('Install the `gh` CLI to make requests to GitHub')); |
| b69ab31 | | | 93 | error.stack = diffFetchError.stack; |
| b69ab31 | | | 94 | return { |
| b69ab31 | | | 95 | title: <T>Unable to fetch data from Github</T>, |
| b69ab31 | | | 96 | error, |
| b69ab31 | | | 97 | buttons: [learnAboutGhButton], |
| b69ab31 | | | 98 | trackErrorName: 'GhCliNotInstalled', |
| b69ab31 | | | 99 | }; |
| b69ab31 | | | 100 | } |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | return { |
| b69ab31 | | | 103 | title: <T>Failed to fetch Diffs</T>, |
| b69ab31 | | | 104 | error: diffFetchError, |
| b69ab31 | | | 105 | trackErrorName: 'DiffFetchFailed', |
| b69ab31 | | | 106 | }; |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | return undefined; |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | export function TopLevelErrors() { |
| b69ab31 | | | 113 | const reconnectStatus = useAtomValue(reconnectingStatus); |
| b69ab31 | | | 114 | const repoInfo = useAtomValue(repositoryInfoOrError); |
| b69ab31 | | | 115 | const diffFetchError = useAtomValue(allDiffSummaries).error; |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | const info = computeTopLevelError(repoInfo, reconnectStatus, diffFetchError); |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | if (info == null) { |
| b69ab31 | | | 120 | return null; |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | return <TrackedError info={info} />; |
| b69ab31 | | | 124 | } |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | function TrackedError({info}: {info: TopLevelErrorInfo}) { |
| b69ab31 | | | 127 | useThrottledEffect( |
| b69ab31 | | | 128 | () => { |
| b69ab31 | | | 129 | if (info.trackErrorName != null) { |
| b69ab31 | | | 130 | tracker.error('TopLevelErrorShown', info.trackErrorName, info.error); |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | }, |
| b69ab31 | | | 133 | 1_000, |
| b69ab31 | | | 134 | [info.trackErrorName, info.error], |
| b69ab31 | | | 135 | ); |
| b69ab31 | | | 136 | return <ErrorNotice title={info.title} error={info.error} buttons={info.buttons} />; |
| b69ab31 | | | 137 | } |