Skip to content

Commit

Permalink
feat: add init sentry
Browse files Browse the repository at this point in the history
- add init from main
- add init from render
  • Loading branch information
miguelsw committed Apr 11, 2024
1 parent ee15ddb commit 99cd2bd
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/apps/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function scheduleHeathCheck() {
// Logger.debug('Health check succeeded');
})
.catch(() => {
Logger.warn('Health check failed, relaunching the worker');
const warning = 'Health check failed, relaunching the worker';
Logger.warn(warning);
Sentry.captureMessage(warning);
workerIsRunning = false;
worker?.destroy();
spawnSyncEngineWorker();

Check warning on line 51 in src/apps/main/background-processes/sync-engine.ts

View workflow job for this annotation

GitHub Actions / 🧪 Lint and test

'spawnSyncEngineWorker' was used before it was defined
Expand Down
1 change: 1 addition & 0 deletions src/apps/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Logger.log(`Running ${packageJson.version}`);

Logger.log('Initializing Sentry for main process');
if (process.env.SENTRY_DSN) {
Logger.log(`App is packaged: ${app.isPackaged}`);
Sentry.init({
// Enable Sentry only when app is packaged
enabled: app.isPackaged,
Expand Down
5 changes: 3 additions & 2 deletions src/apps/main/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { io, Socket } from 'socket.io-client';
import { obtainToken } from './auth/service';
import eventBus from './event-bus';
import { broadcastToWindows } from './windows';
import { reportError } from '../renderer/utils/errors';
import * as Sentry from '@sentry/electron/main';

type XHRRequest = {
getResponseHeader: (headerName: string) => string[] | null;
Expand Down Expand Up @@ -54,11 +54,12 @@ function cleanAndStartRemoteNotifications() {

socket.on('disconnect', (reason) => {
logger.log('❌ Remote notifications disconnected, reason: ', reason);
Sentry.captureException(reason);
});

socket.on('connect_error', (error) => {
logger.error('❌ Remote notifications connect error: ', error);
reportError(error);
Sentry.captureException(error);
});

socket.on('event', (data) => {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/renderer/hooks/VirtualDriveStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { reportError } from '../utils/errors';
import { reportError } from '../utils/sentry';
import { VirtualDriveStatus } from '../../shared/types/VirtualDriveStatus';

export default function useVirtualDriveStatus() {
Expand Down
2 changes: 2 additions & 0 deletions src/apps/renderer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { render } from 'react-dom';
import App from './App';
import { initSentry } from './utils/sentry';
initSentry();

render(<App />, document.getElementById('root'));
2 changes: 1 addition & 1 deletion src/apps/renderer/pages/Feedback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslationContext } from '../../context/LocalContext';
import { ChatsCircle } from 'phosphor-react';
import WindowTopBar from '../../components/WindowTopBar';
import TextArea from '../../components/TextArea';
import { reportError } from '../../utils/errors';
import { reportError } from '../../utils/sentry';

const CHARACTERS_LIMIT = 1000;
export default function Feedback() {
Expand Down
5 changes: 4 additions & 1 deletion src/apps/renderer/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Button from '../../components/Button';
import PasswordInput from '../../components/PasswordInput';
import TextInput from '../../components/TextInput';
import WindowTopBar from '../../components/WindowTopBar';
import { reportError, setUserContextForReports } from '../../utils/sentry';

const TOWFA_ERROR_MESSAGE = 'Wrong 2-factor auth code';

Expand Down Expand Up @@ -38,18 +39,20 @@ export default function Login() {

try {
const res = await accessRequest(email, password, encryptedHash, twoFA);
setUserContextForReports(res.user);
window.electron.userLoggedIn(res);
} catch (err) {
const { message } = err as Error;

const phaseToSet =
message === TOWFA_ERROR_MESSAGE ? '2fa' : 'credentials';
message === TOWFA_ERROR_MESSAGE ? '2fa' : 'credentials';

setState('error');
setPhase(phaseToSet);
// TODO: adjust styles to acomodate longer error messages
setErrorDetails(translate('login.2fa.wrong-code'));
window.electron.userLogginFailed(email);
reportError(err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/apps/renderer/pages/Migration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SLIDES } from './config';
import { MigrationSlideProps } from './helpers';
import { useTranslationContext } from '../../context/LocalContext';
import useClientPlatform from '../../hooks/ClientPlatform';
import { reportError } from '../../utils/errors';
import { reportError } from '../../utils/sentry';

const totalSlides = SLIDES.length - 3;

Expand Down
2 changes: 1 addition & 1 deletion src/apps/renderer/pages/Widget/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useGeneralIssues from '../../hooks/GeneralIssues';
import useProcessIssues from '../../hooks/ProcessIssues';
import useUsage from '../../hooks/useUsage';
import useVirtualDriveStatus from '../../hooks/VirtualDriveStatus';
import { reportError } from '../../utils/errors';
import { reportError } from '../../utils/sentry';

export default function Header() {
const { translate } = useTranslationContext();
Expand Down
14 changes: 0 additions & 14 deletions src/apps/renderer/utils/errors.ts

This file was deleted.

44 changes: 44 additions & 0 deletions src/apps/renderer/utils/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as Sentry from '@sentry/electron/renderer';
import { User } from '../../main/types';

/**
* Init Sentry in the renderer process
* @param dsn Sentry DSN
* @param enabled Whether Sentry should be enabled or not
*/
export const initSentry = () => {
Sentry.init({
dsn: process.env.SENTRY_DSN,
enabled: true, // TODO: add env variable to enable/disable Sentry
});
Sentry.captureMessage('Renderer process started');
};

/**
* Reports an error to Sentry from the renderer process
*
* @param error The error to be reported
* @param context The context to attach to the error such the userId, tags, boolean values...
*/
export const reportError = (
error: unknown,
context: Record<string, string> = {}
) => {
Sentry.captureException(error, context);
};


/**
* Set user context in Sentry
* @param user User object
*/
export const setUserContextForReports = (user: User) => {
if (!user) {
Sentry.setUser(null);
return;
}
Sentry.setUser({
email: user.email,
id: user.uuid,
});
};

0 comments on commit 99cd2bd

Please sign in to comment.