diff --git a/src/lib/api.ts b/src/lib/api.ts index 142b72079..0b5bc25e5 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -6,7 +6,8 @@ import { } from '@apollo/client/core'; import { setContext } from '@apollo/client/link/context'; import { ApolloLink } from '@apollo/client/link/core'; -import { onError } from '@apollo/client/link/error'; +import { ErrorResponse, onError } from '@apollo/client/link/error'; +import { ServerError } from '@apollo/client/link/utils'; import chalk from 'chalk'; import http from './api/http'; @@ -30,6 +31,15 @@ export function enableGlobalGraphQLErrorHandling(): void { globalGraphQLErrorHandlingEnabled = true; } +function isServerError( + networkError: ErrorResponse[ 'networkError' ] +): networkError is ServerError { + if ( ! networkError ) { + return false; + } + return 'result' in networkError; +} + export default function API( { exitOnError = true, }: { @@ -37,10 +47,16 @@ export default function API( { } = {} ): ApolloClient< NormalizedCacheObject > { const errorLink = onError( ( { networkError, graphQLErrors } ) => { if ( networkError && 'statusCode' in networkError && networkError.statusCode === 401 ) { - console.error( - chalk.red( 'Unauthorized:' ), - 'You are unauthorized to perform this request, please logout with `vip logout` then try again.' - ); + let message = + 'You are not authorized to perform this request; please logout with `vip logout`, then try again.'; + if ( + isServerError( networkError ) && + networkError.result?.code === 'token-disabled-inactivity' + ) { + message = + 'Your token has been disabled due to inactivity; please log out with `vip logout`, then try again.'; + } + console.error( chalk.red( 'Unauthorized:' ), message ); process.exit( 1 ); }