Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/renderer/__mocks__/notifications-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { StateType, Subject, SubjectType } from '../typesGitHub';
import {
mockEnterpriseNotifications,
mockGitHubNotifications,
mockSingleNotification,
} from '../utils/api/__mocks__/response-mocks';
import {
mockGitHubCloudAccount,
Expand All @@ -25,7 +26,7 @@ export const mockAccountNotifications: AccountNotifications[] = [
export const mockSingleAccountNotifications: AccountNotifications[] = [
{
account: mockGitHubCloudAccount,
notifications: [mockGitHubNotifications[0]],
notifications: [mockSingleNotification],
error: null,
},
];
Expand Down
19 changes: 10 additions & 9 deletions src/renderer/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { act, renderHook, waitFor } from '@testing-library/react';
import axios, { AxiosError } from 'axios';
import nock from 'nock';

import * as logger from '../../shared/logger';

import {
mockAuth,
mockGitHubCloudAccount,
Expand All @@ -16,16 +14,19 @@ import {
mockSingleNotification,
} from '../utils/api/__mocks__/response-mocks';
import { Errors } from '../utils/errors';
import * as logger from '../utils/logger';
import { useNotifications } from './useNotifications';

describe('renderer/hooks/useNotifications.ts', () => {
const logErrorSpy = jest.spyOn(logger, 'logError').mockImplementation();
const rendererLogErrorSpy = jest
.spyOn(logger, 'rendererLogError')
.mockImplementation();

beforeEach(() => {
// axios will default to using the XHR adapter which can't be intercepted
// by nock. So, configure axios to use the node adapter.
axios.defaults.adapter = 'http';
logErrorSpy.mockReset();
rendererLogErrorSpy.mockReset();
});

const id = mockSingleNotification.id;
Expand Down Expand Up @@ -300,7 +301,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBe(Errors.BAD_CREDENTIALS);
expect(logErrorSpy).toHaveBeenCalledTimes(4);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(4);
});

it('should fetch notifications with different failures', async () => {
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBeNull();
expect(logErrorSpy).toHaveBeenCalledTimes(4);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(4);
});
});

Expand Down Expand Up @@ -386,7 +387,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

expect(result.current.notifications.length).toBe(0);
expect(logErrorSpy).toHaveBeenCalledTimes(1);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -429,7 +430,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

expect(result.current.notifications.length).toBe(0);
expect(logErrorSpy).toHaveBeenCalledTimes(1);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -521,7 +522,7 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

expect(result.current.notifications.length).toBe(0);
expect(logErrorSpy).toHaveBeenCalledTimes(1);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
});
});
});
9 changes: 4 additions & 5 deletions src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useCallback, useState } from 'react';

import { logError } from '../../shared/logger';

import type {
Account,
AccountNotifications,
Expand All @@ -17,6 +15,7 @@ import {
} from '../utils/api/client';
import { updateTrayIcon } from '../utils/comms';
import { isMarkAsDoneFeatureSupported } from '../utils/features';
import { rendererLogError } from '../utils/logger';
import { triggerNativeNotifications } from '../utils/notifications/native';
import {
getAllNotifications,
Expand Down Expand Up @@ -129,7 +128,7 @@ export const useNotifications = (): NotificationsState => {
setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
} catch (err) {
logError(
rendererLogError(
'markNotificationsAsRead',
'Error occurred while marking notifications as read',
err,
Expand Down Expand Up @@ -167,7 +166,7 @@ export const useNotifications = (): NotificationsState => {
setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
} catch (err) {
logError(
rendererLogError(
'markNotificationsAsDone',
'Error occurred while marking notifications as done',
err,
Expand Down Expand Up @@ -196,7 +195,7 @@ export const useNotifications = (): NotificationsState => {
await markNotificationsAsRead(state, [notification]);
}
} catch (err) {
logError(
rendererLogError(
'unsubscribeNotification',
'Error occurred while unsubscribing from notification thread',
err,
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/routes/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import {
Text,
} from '@primer/react';

import { logError } from '../../shared/logger';

import { AvatarWithFallback } from '../components/avatars/AvatarWithFallback';
import { Contents } from '../components/layout/Contents';
import { Page } from '../components/layout/Page';
Expand All @@ -44,6 +42,7 @@ import {
openDeveloperSettings,
openHost,
} from '../utils/links';
import { rendererLogError } from '../utils/logger';
import { saveState } from '../utils/storage';

export const AccountsRoute: FC = () => {
Expand Down Expand Up @@ -98,7 +97,7 @@ export const AccountsRoute: FC = () => {
try {
await loginWithGitHubApp();
} catch (err) {
logError('loginWithGitHub', 'failed to login with GitHub', err);
rendererLogError('loginWithGitHub', 'failed to login with GitHub', err);
}
}, []);

Expand Down
9 changes: 6 additions & 3 deletions src/renderer/routes/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { useNavigate } from 'react-router-dom';
import { KeyIcon, MarkGithubIcon, PersonIcon } from '@primer/octicons-react';
import { Button, Heading, Stack, Text } from '@primer/react';

import { logError } from '../../shared/logger';

import { LogoIcon } from '../components/icons/LogoIcon';
import { Centered } from '../components/layout/Centered';
import { AppContext } from '../context/App';
import { Size } from '../types';
import { showWindow } from '../utils/comms';
import { rendererLogError } from '../utils/logger';

export const LoginRoute: FC = () => {
const navigate = useNavigate();
Expand All @@ -27,7 +26,11 @@ export const LoginRoute: FC = () => {
try {
await loginWithGitHubApp();
} catch (err) {
logError('loginWithGitHubApp', 'failed to login with GitHub', err);
rendererLogError(
'loginWithGitHubApp',
'failed to login with GitHub',
err,
);
}
}, [loginWithGitHubApp]);

Expand Down
9 changes: 6 additions & 3 deletions src/renderer/routes/LoginWithOAuthApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
} from '@primer/react';
import { Banner } from '@primer/react/experimental';

import { logError } from '../../shared/logger';

import { Contents } from '../components/layout/Contents';
import { Page } from '../components/layout/Page';
import { Footer } from '../components/primitives/Footer';
Expand All @@ -35,6 +33,7 @@ import {
} from '../utils/auth/utils';
import { openExternalLink } from '../utils/comms';
import { Constants } from '../utils/constants';
import { rendererLogError } from '../utils/logger';

interface IFormData {
hostname?: Hostname;
Expand Down Expand Up @@ -120,7 +119,11 @@ export const LoginWithOAuthAppRoute: FC = () => {
await loginWithOAuthApp(data as LoginOAuthAppOptions);
navigate(-1);
} catch (err) {
logError('loginWithOAuthApp', 'Failed to login with OAuth App', err);
rendererLogError(
'loginWithOAuthApp',
'Failed to login with OAuth App',
err,
);
setErrors({
invalidCredentialsForHost: `Failed to validate provided Client ID and Secret against ${data.hostname}`,
});
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/routes/LoginWithPersonalAccessToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
} from '@primer/react';
import { Banner } from '@primer/react/experimental';

import { logError } from '../../shared/logger';

import { Contents } from '../components/layout/Contents';
import { Page } from '../components/layout/Page';
import { Footer } from '../components/primitives/Footer';
Expand All @@ -35,6 +33,7 @@ import {
} from '../utils/auth/utils';
import { openExternalLink } from '../utils/comms';
import { Constants } from '../utils/constants';
import { rendererLogError } from '../utils/logger';

interface IFormData {
token?: Token;
Expand Down Expand Up @@ -112,7 +111,7 @@ export const LoginWithPersonalAccessTokenRoute: FC = () => {
);
navigate(-1);
} catch (err) {
logError(
rendererLogError(
'loginWithPersonalAccessToken',
'Failed to login with PAT',
err,
Expand Down
9 changes: 5 additions & 4 deletions src/renderer/utils/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import axios, { type AxiosPromise, type AxiosResponse } from 'axios';

import * as logger from '../../../shared/logger';

import {
mockGitHubCloudAccount,
mockGitHubEnterpriseServerAccount,
mockToken,
} from '../../__mocks__/state-mocks';
import type { Hostname, Link, SettingsState, Token } from '../../types';
import * as logger from '../../utils/logger';
import {
getAuthenticatedUser,
getHtmlUrl,
Expand Down Expand Up @@ -322,7 +321,9 @@ describe('renderer/utils/api/client.ts', () => {
});

it('should handle error', async () => {
const logErrorSpy = jest.spyOn(logger, 'logError').mockImplementation();
const rendererLogErrorSpy = jest
.spyOn(logger, 'rendererLogError')
.mockImplementation();

const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');

Expand All @@ -335,7 +336,7 @@ describe('renderer/utils/api/client.ts', () => {
'123' as Token,
);

expect(logErrorSpy).toHaveBeenCalledTimes(1);
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(1);
});
});
});
7 changes: 3 additions & 4 deletions src/renderer/utils/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { AxiosPromise } from 'axios';
import { print } from 'graphql/language/printer';

import { logError } from '../../../shared/logger';

import type {
Account,
Hostname,
Expand All @@ -25,6 +23,7 @@ import type {
UserDetails,
} from '../../typesGitHub';
import { isAnsweredDiscussionFeatureSupported } from '../features';
import { rendererLogError } from '../logger';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatAsGitHubSearchSyntax } from './graphql/utils';
import { apiRequestAuth } from './request';
Expand Down Expand Up @@ -219,7 +218,7 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
const response = (await apiRequestAuth(url, 'GET', token)).data;
return response.html_url;
} catch (err) {
logError(
rendererLogError(
'getHtmlUrl',
`error occurred while fetching html url for ${url}`,
err,
Expand Down Expand Up @@ -274,7 +273,7 @@ export async function getLatestDiscussion(
)[0] ?? null
);
} catch (err) {
logError(
rendererLogError(
'getLatestDiscussion',
'failed to fetch latest discussion for notification',
err,
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/utils/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import axios, {
type Method,
} from 'axios';

import { logError } from '../../../shared/logger';

import type { Link, Token } from '../../types';
import { decryptValue } from '../comms';
import { rendererLogError } from '../logger';
import { getNextURLFromLinkHeader } from './utils';

/**
Expand Down Expand Up @@ -70,7 +69,7 @@ export async function apiRequestAuth(
nextUrl = getNextURLFromLinkHeader(response);
}
} catch (err) {
logError('apiRequestAuth', 'API request failed:', err);
rendererLogError('apiRequestAuth', 'API request failed:', err);

throw err;
}
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/utils/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import semver from 'semver';

import { APPLICATION } from '../../../shared/constants';
import { namespacedEvent } from '../../../shared/events';
import { logError, logInfo, logWarn } from '../../../shared/logger';

import type {
Account,
Expand All @@ -23,6 +22,7 @@ import { apiRequest } from '../api/request';
import { encryptValue, openExternalLink } from '../comms';
import { Constants } from '../constants';
import { getPlatformFromHostname } from '../helpers';
import { rendererLogError, rendererLogInfo, rendererLogWarn } from '../logger';
import type { AuthMethod, AuthResponse, AuthTokenResponse } from './types';

export function authGitHub(
Expand Down Expand Up @@ -69,7 +69,7 @@ export function authGitHub(
ipcRenderer.on(
namespacedEvent('auth-callback'),
(_, callbackUrl: string) => {
logInfo(
rendererLogInfo(
'renderer:auth-callback',
`received authentication callback URL ${callbackUrl}`,
);
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function addAccount(
);

if (accountAlreadyExists) {
logWarn(
rendererLogWarn(
'addAccount',
`account for user ${newAccount.user.login} already exists`,
);
Expand Down Expand Up @@ -189,13 +189,13 @@ export async function refreshAccount(account: Account): Promise<Account> {
);

if (!account.hasRequiredScopes) {
logWarn(
rendererLogWarn(
'refreshAccount',
`account for user ${account.user.login} is missing required scopes`,
);
}
} catch (err) {
logError(
rendererLogError(
'refreshAccount',
`failed to refresh account for user ${account.user.login}`,
err,
Expand Down
Loading
Loading