Skip to content

Commit

Permalink
refactor: choose some more expressive names in errorHandling.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMa committed Feb 12, 2025
1 parent a8a327a commit 400a788
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/ElectronBackend/errorHandling/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getGlobalBackendState } from '../main/globalBackendState';
import logger from '../main/logger';
import { getLoadedFilePath } from '../utils/getLoadedFile';

async function reportListenerErrorInBackend(
async function showListenerErrorInMessageBox(
mainWindow: BrowserWindow,
error: unknown,
): Promise<void> {
Expand All @@ -40,17 +40,18 @@ async function reportListenerErrorInBackend(
}
}

function reportListenerErrorInFrontend(_: BrowserWindow, error: unknown): void {
function sendListenerErrorToFrontend(_: BrowserWindow, error: unknown): void {
// NOTE: these log messages are forwarded to the frontend
if (error instanceof Error) {
logger.error(error.message);
} else {
logger.error('Unexpected internal error');
}
}

export const ListenerErrorReporter = {
Backend: reportListenerErrorInBackend,
Frontend: reportListenerErrorInFrontend,
export const ListenerErrorReporting = {
ShowMessageBox: showListenerErrorInMessageBox,
SendToFrontend: sendListenerErrorToFrontend,
};

type FuncType<T> = T extends (...args: infer P) => infer R
Expand All @@ -68,7 +69,7 @@ export function createVoidListenerCallbackWithErrorHandling<F>(
reportError: (
mainWindow: BrowserWindow,
error: unknown,
) => Promise<void> | void = reportListenerErrorInBackend,
) => Promise<void> | void = showListenerErrorInMessageBox,
): (...args: FTParameters<F>) => Promise<void> {
return async (...args: FTParameters<F>): Promise<void> => {
try {
Expand All @@ -86,7 +87,7 @@ export function createListenerCallbackWithErrorHandling<F>(
reportError: (
mainWindow: BrowserWindow,
error: unknown,
) => Promise<void> | void = reportListenerErrorInBackend,
) => Promise<void> | void = showListenerErrorInMessageBox,
): (...args: FTParameters<F>) => Promise<ReturnTypeWithoutPromise<F>> {
return async (
...args: FTParameters<F>
Expand Down
2 changes: 1 addition & 1 deletion src/ElectronBackend/main/__tests__/listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ describe('getImportFileSelectSaveLocationListener', () => {
expect(returnedFilePath).toBe(selectedFilePath);
});

it('returns null when no save location was selected', async () => {
it('returns an empty string when no save location was selected', async () => {
const mainWindow = await initWindowAndBackendState();

const listener = getImportFileSelectSaveLocationListener(mainWindow);
Expand Down
4 changes: 2 additions & 2 deletions src/ElectronBackend/main/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { LoadedFileFormat } from '../enums/enums';
import {
createListenerCallbackWithErrorHandling,
createVoidListenerCallbackWithErrorHandling,
ListenerErrorReporter,
ListenerErrorReporting,
} from '../errorHandling/errorHandling';
import { loadInputAndOutputFromFilePath } from '../input/importFromFile';
import { serializeAttributions } from '../input/parseInputData';
Expand Down Expand Up @@ -236,7 +236,7 @@ export function getImportFileConvertAndLoadListener(

return true;
},
ListenerErrorReporter.Frontend,
ListenerErrorReporting.SendToFrontend,
);
}

Expand Down

0 comments on commit 400a788

Please sign in to comment.