Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide dedicated error message for when a token is marked as disabled due to inactivity #2084

Merged
merged 9 commits into from
Nov 25, 2024
26 changes: 21 additions & 5 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -30,17 +31,32 @@ 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,
}: {
exitOnError?: boolean;
} = {} ): 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 );
}

Expand Down