Skip to content

Commit

Permalink
refactor: remove the deprecated sendError
Browse files Browse the repository at this point in the history
Logging the error inline makes it more clear and what is happening.

Signed-off-by: Alexander Alemayhu <[email protected]>
  • Loading branch information
aalemayhu committed Dec 31, 2024
1 parent 7f8a14d commit 591ffe3
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 69 deletions.
10 changes: 6 additions & 4 deletions src/controllers/CardOptionsController/CardOptionsController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Request, Response } from 'express';
import { sendError } from '../../lib/error/sendError';
import { IServiceSettings } from '../../services/SettingsService';
import { getOwner } from '../../lib/User/getOwner';
import supportedOptions from './supportedOptions';
Expand All @@ -22,7 +21,8 @@ class CardOptionsController {
});
res.status(200).send();
} catch (error) {
sendError(error);
console.info('Create setting failed');
console.error(error);
res.status(400).send();
}
}
Expand All @@ -35,7 +35,8 @@ class CardOptionsController {
await this.service.delete(owner, id);
res.status(200).send();
} catch (error) {
sendError(error);
console.info('Delete setting failed');
console.error(error);
res.status(400).send();
}
}
Expand All @@ -52,7 +53,8 @@ class CardOptionsController {
const storedSettings = await this.service.getById(id);
return res.json({ payload: storedSettings });
} catch (error) {
sendError(error);
console.info('Get setting failed');
console.error(error);
res.status(400).send();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/DownloadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs';
import { Request, Response } from 'express';

import path from 'path';
import { sendError } from '../lib/error/sendError';
import StorageHandler from '../lib/storage/StorageHandler';
import DownloadService from '../services/DownloadService';
import { canAccess } from '../lib/misc/canAccess';
Expand Down Expand Up @@ -34,7 +33,8 @@ class DownloadController {
this.service.deleteMissingFile(owner, key);
res.redirect('/uploads');
} else {
sendError(error);
console.info('Download failed');
console.error(error);
res
.status(404)
.send(
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/JobController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import express from 'express';

import { sendError } from '../lib/error/sendError';
import JobService from '../services/JobService';
import { getOwner } from '../lib/User/getOwner';

Expand All @@ -20,7 +19,8 @@ class JobController {
res.status(200).send();
} catch (error) {
res.status(500).send();
sendError(error);
console.info('Delete job failed');
console.error(error);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/controllers/NotionController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Request, Response } from 'express';

import { sendError } from '../lib/error/sendError';
import performConversion from '../lib/storage/jobs/helpers/performConversion';
import CardOption from '../lib/parser/Settings';
import BlockHandler from '../services/NotionService/BlockHandler/BlockHandler';
Expand Down Expand Up @@ -30,7 +29,8 @@ class NotionController {
await this.service.connectToNotion(authorizationCode, res.locals.owner);
return res.redirect('/search');
} catch (err) {
sendError(err);
console.info('Connect to Notion failed');
console.error(err);
return res.redirect('/search');
}
}
Expand Down Expand Up @@ -175,7 +175,8 @@ class NotionController {
);
return res.json(database);
} catch (error) {
sendError(error);
console.info('Get database failed');
console.error(error);
res.status(500).send();
}
}
Expand All @@ -195,8 +196,8 @@ class NotionController {
const deletion = await this.service.disconnect(res.locals.owner);
res.status(200).send({ didDelete: deletion });
} catch (err) {
sendError(err);
console.debug('Failed to disconnect');
console.info('Disconnect from Notion failed');
console.error(err);
res.status(500).send({ didDelete: false });
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/ParserRulesController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Request, Response } from 'express';

import { sendError } from '../lib/error/sendError';
import ParserRulesService from '../services/ParserRulesService';
import { getOwner } from '../lib/User/getOwner';

Expand All @@ -22,7 +21,8 @@ class RulesController {
);
res.status(200).send(result);
} catch (error) {
sendError(error);
console.info('Create rule failed');
console.error(error);
res.status(400).send();
}
}
Expand All @@ -38,7 +38,8 @@ class RulesController {
const rule = await this.service.getById(id);
res.status(200).json(rule);
} catch (err) {
sendError(err);
console.info('Get rule failed');
console.error(err);
res.status(200).json();
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/controllers/Upload/UploadController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express from 'express';

import { getOwner } from '../../lib/User/getOwner';
import { sendError } from '../../lib/error/sendError';
import NotionService from '../../services/NotionService';
import UploadService from '../../services/UploadService';
import { getUploadHandler } from '../../lib/misc/GetUploadHandler';
Expand All @@ -28,7 +27,8 @@ class UploadController {
await this.service.deleteUpload(owner, key);
await this.notionService.purgeBlockCache(owner);
} catch (error) {
sendError(error);
console.info('Delete upload failed');
console.error(error);
return res.status(500).send();
}

Expand All @@ -41,7 +41,8 @@ class UploadController {
const uploads = await this.service.getUploadsByOwner(owner);
res.json(uploads);
} catch (error) {
sendError(error);
console.info('Get uploads failed');
console.error(error);
res.status(400);
}
}
Expand All @@ -58,7 +59,8 @@ class UploadController {
await this.service.handleUpload(req, res);
});
} catch (error) {
sendError(error);
console.info('Upload file failed');
console.error(error);
res.status(400);
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/controllers/UsersControllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import express from 'express';

import { sendError } from '../lib/error/sendError';
import AuthenticationService from '../services/AuthenticationService';
import UsersService from '../services/UsersService';
import { getRedirect } from './helpers/getRedirect';
Expand Down Expand Up @@ -35,7 +34,8 @@ class UsersController {
);
res.status(200).send({ message: 'ok' });
} catch (error) {
sendError(error);
console.info('Update password failed');
console.error(error);
next(new Error('Failed to create new password.'));
}
}
Expand All @@ -56,7 +56,8 @@ class UsersController {
await this.userService.sendResetEmail(email, this.authService);
return res.status(200).json({ message: 'ok' });
} catch (error) {
sendError(error);
console.info('Send reset email failed');
console.error(error);
next(error);
}
}
Expand All @@ -72,7 +73,8 @@ class UsersController {
res.clearCookie('token');
res.redirect('/');
} catch (error) {
sendError(error);
console.info('Log out failed');
console.error(error);
next(error);
}
}
Expand Down Expand Up @@ -111,7 +113,8 @@ class UsersController {
res.status(200).json({ token, redirect: getRedirect(req) });
}
} catch (error) {
sendError(error);
console.info('Login failed');
console.error(error);
next(
new Error('Failed to login, please try again or register your account.')
);
Expand Down Expand Up @@ -144,7 +147,8 @@ class UsersController {
);
res.status(200).json({ message: 'ok' });
} catch (error) {
sendError(error);
console.info('Register failed');
console.error(error);
return next(error);
}
}
Expand All @@ -162,7 +166,8 @@ class UsersController {
}
return res.redirect('/login');
} catch (err) {
sendError(err);
console.info('Reset password failed');
console.error(err);
next(err);
}
}
Expand Down Expand Up @@ -209,8 +214,8 @@ class UsersController {
await this.userService.updateSubscriptionLinkedEmail(owner, email);
return res.status(200).json({});
} catch (error) {
console.info('Link email failed');
console.error(error);
sendError(error);
return res.status(500).json({ message: 'Failed to link email' });
}
}
Expand All @@ -225,7 +230,8 @@ class UsersController {
await this.userService.deleteUser(owner);
res.status(200).json({});
} catch (error) {
sendError(error);
console.info('Delete account failed');
console.error(error);
return res.status(500).json({ message: 'Failed to delete account' });
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/anki/CardGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { homedir } from 'os';
import path from 'path';

import { CREATE_DECK_SCRIPT_PATH, resolvePath } from '../constants';
import { sendError } from '../error/sendError';

function PYTHON() {
const os = process.platform;
Expand Down Expand Up @@ -36,7 +35,8 @@ class CardGenerator {
});

process.on('error', (err) => {
sendError(err);
console.info('Create deck failed');
console.error(err);
reject(err);
});

Expand Down
11 changes: 0 additions & 11 deletions src/lib/error/sendError.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import replaceAll from './helpers/replaceAll';

import get16DigitRandomId from '../../shared/helpers/get16DigitRandomId';
import { isValidAudioFile } from '../anki/format';
import { sendError } from '../error/sendError';
import FallbackParser from './experimental/FallbackParser';
import { embedFile } from './exporters/embedFile';
import getYouTubeEmbedLink from './helpers/getYouTubeEmbedLink';
Expand Down Expand Up @@ -164,7 +163,7 @@ export class DeckParser {
`;
} catch (error) {
console.info('experienced error while getting link');
sendError(error);
console.error(error);
return null;
}
}
Expand Down Expand Up @@ -275,7 +274,7 @@ export class DeckParser {
return getYouTubeID(input);
} catch (error) {
console.debug('error in getYouTubeID');
sendError(error);
console.error(error);
return null;
}
});
Expand All @@ -299,7 +298,7 @@ export class DeckParser {
return m[0].split('">')[0];
} catch (error) {
console.debug('error in getSoundCloudURL');
sendError(error);
console.error(error);
return null;
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/parser/Settings/CardOption.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parseTemplate } from './helpers/parseTemplate';

import { UserSuppliedTemplateFile } from './types';
import { sendError } from '../../error/sendError';

class CardOption {
readonly deckName: string | undefined;
Expand Down Expand Up @@ -121,7 +120,8 @@ class CardOption {
this.n2aCloze = parseTemplate(input['n2a-cloze']);
this.n2aInput = parseTemplate(input['n2a-input']);
} catch (error) {
sendError(error);
console.info('Retrieve templates failed');
console.error(error);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/parser/Settings/loadSettingsFromDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Knex } from 'knex';

import { getCustomTemplate } from './helpers/getCustomTemplate';
import { sendError } from '../../error/sendError';
import CardOption from './CardOption';

export const loadSettingsFromDatabase = async (
Expand Down Expand Up @@ -42,7 +41,8 @@ export const loadSettingsFromDatabase = async (
}
return settings;
} catch (error: unknown) {
sendError(error);
console.info('Load settings from database failed');
console.error(error);
}
return new CardOption(CardOption.LoadDefaultOptions());
};
10 changes: 6 additions & 4 deletions src/lib/storage/StorageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import aws from 'aws-sdk';
import { ObjectList } from 'aws-sdk/clients/s3';
import { sendError } from '../error/sendError';

class StorageHandler {
s3: aws.S3;
Expand Down Expand Up @@ -42,7 +41,8 @@ class StorageHandler {
.promise();
return true;
} catch (err) {
sendError(err);
console.info('Delete file failed');
console.error(err);
return false;
}
}
Expand All @@ -68,7 +68,8 @@ class StorageHandler {
}
} catch (err) {
if (err) {
sendError(err);
console.info('Get contents failed');
console.error(err);
return reject(err);
}
}
Expand Down Expand Up @@ -108,7 +109,8 @@ class StorageHandler {
},
(err, response) => {
if (err) {
sendError(err);
console.info('Upload file failed');
console.error(err);
reject(err);
} else {
resolve(response);
Expand Down
Loading

0 comments on commit 591ffe3

Please sign in to comment.