From ccf2a3e7f06a92e99c57a0f324d1c8bb48ebe375 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Sat, 12 Aug 2023 16:55:25 +0200 Subject: [PATCH] refactored .thens as try catch --- database-calls/db.mjs | 23 ++-- database-calls/getAcceptablePrompts.mjs | 8 +- database-calls/insertPublished.mjs | 100 +++++++++--------- .../todaysPromptAlreadyPublished.mjs | 12 ++- 4 files changed, 79 insertions(+), 64 deletions(-) diff --git a/database-calls/db.mjs b/database-calls/db.mjs index 5b19dc8..b01a092 100644 --- a/database-calls/db.mjs +++ b/database-calls/db.mjs @@ -15,22 +15,27 @@ export const getWords = async () => { return db .select("word") .from("dictionary") - .catch(error=>{throw error}) + .catch(error => { throw error }) } -export const valueExistsInTable = async (table,column,value) =>{ - const number = await db(table) - .count('* as count') - .where(column, value) - .catch(error => { throw error }) - return number[0].count > 0 +export const valueExistsInTable = async (table, column, value) => { + try { + const number = await db(table) + .count('* as count') + .where(column, value) + return number[0].count > 0 + } catch (error) { + logger.error("valueExistsInTable failed!") + throw error + } + } export const todaysPromptAlreadyPublished = async () => { - return valueExistsInTable('published','date',isoDate()) + return valueExistsInTable('published', 'date', isoDate()) } export const wordIsAlreadyInBuffer = async (word) => { - return valueExistsInTable('buffer','word',word) + return valueExistsInTable('buffer', 'word', word) } \ No newline at end of file diff --git a/database-calls/getAcceptablePrompts.mjs b/database-calls/getAcceptablePrompts.mjs index b1f524d..aaedf67 100644 --- a/database-calls/getAcceptablePrompts.mjs +++ b/database-calls/getAcceptablePrompts.mjs @@ -10,7 +10,8 @@ const blocklist = db.union([ export default async () => { logger.trace("getAcceptablePrompt called") - return db('dictionary') + try { + return db('dictionary') .select('*') .where({ derivative: 0, @@ -22,5 +23,8 @@ export default async () => { .whereRaw('length(word) > 3') .whereNotNull('pronunciation') .orderByRaw('count desc') - .catch(error=>{throw error}) + } catch (error) { + logger.error("getAcceptablePrompts failed!") + throw error + } } \ No newline at end of file diff --git a/database-calls/insertPublished.mjs b/database-calls/insertPublished.mjs index c5ff660..b4f2e87 100644 --- a/database-calls/insertPublished.mjs +++ b/database-calls/insertPublished.mjs @@ -2,62 +2,62 @@ import { db } from "./db.mjs" import { isoDate } from "../utilities.mjs" import logger from "../logger.mjs" const sampleRes = { - createdNote: { - id: '9i8urek4jtzhmtuh', - createdAt: '2023-08-10T15:02:35.380Z', - userId: '9i5z5o9zy11l3skl', - user: { - id: '9i5z5o9zy11l3skl', - name: null, - username: 'micro365', - host: null, - avatarUrl: 'https://localhost:3000/files/thumbnail-4e0e8b82-df72-48f7-8100-b7515173da9d', - avatarBlurhash: 'ySPjGct7xu%M-;xu-;%MRjWBoffQofWB~qRjRjayRjfQM{M{t7ofWBt7ayfQ~qj[WBj[M{WBof?bofayfQM{WBfQt7xuofWBofofM{', - avatarColor: null, - isLocked: false, - speakAsCat: true, - emojis: [], - onlineStatus: 'active', - driveCapacityOverrideMb: null - }, - text: "Today's #micro365 prompt is:\n" + - '# $[x2 $[font.serif **marmalade**]]\n' + - '/mˈɑːmɐlˌe‍ɪd/\n' + - '**noun**:\n' + - '- A kind of jam made with citrus fruit, distinguished by being made slightly bitter by the addition of the peel and by partial caramelisation during manufacture. Most commonly made with Seville oranges, and usually qualified by the name of the fruit when made with other types of fruit.\n' + - '- A kind of jam made with citrus fruit, distinguished by being made slightly bitter by the addition of the peel and by partial caramelisation during manufacture. Most commonly made with Seville oranges, and usually qualified by the name of the fruit when made with other types of fruit. Ellipsis of orange marmalade.\n' + - '- quince jam\n' + - '**verb**:\n' + - '- To spread marmalade on.\n' + - '#writing #microfiction #vss #marmalade', - cw: null, - visibility: 'public', - renoteCount: 0, - repliesCount: 0, - reactions: {}, - reactionEmojis: [], + createdNote: { + id: '9i8urek4jtzhmtuh', + createdAt: '2023-08-10T15:02:35.380Z', + userId: '9i5z5o9zy11l3skl', + user: { + id: '9i5z5o9zy11l3skl', + name: null, + username: 'micro365', + host: null, + avatarUrl: 'https://localhost:3000/files/thumbnail-4e0e8b82-df72-48f7-8100-b7515173da9d', + avatarBlurhash: 'ySPjGct7xu%M-;xu-;%MRjWBoffQofWB~qRjRjayRjfQM{M{t7ofWBt7ayfQ~qj[WBj[M{WBof?bofayfQM{WBfQt7xuofWBofofM{', + avatarColor: null, + isLocked: false, + speakAsCat: true, emojis: [], - tags: [ 'micro365', 'writing', 'microfiction', 'vss', 'marmalade' ], - fileIds: [], - files: [], - replyId: null, - renoteId: null - } + onlineStatus: 'active', + driveCapacityOverrideMb: null + }, + text: "Today's #micro365 prompt is:\n" + + '# $[x2 $[font.serif **marmalade**]]\n' + + '/mˈɑːmɐlˌe‍ɪd/\n' + + '**noun**:\n' + + '- A kind of jam made with citrus fruit, distinguished by being made slightly bitter by the addition of the peel and by partial caramelisation during manufacture. Most commonly made with Seville oranges, and usually qualified by the name of the fruit when made with other types of fruit.\n' + + '- A kind of jam made with citrus fruit, distinguished by being made slightly bitter by the addition of the peel and by partial caramelisation during manufacture. Most commonly made with Seville oranges, and usually qualified by the name of the fruit when made with other types of fruit. Ellipsis of orange marmalade.\n' + + '- quince jam\n' + + '**verb**:\n' + + '- To spread marmalade on.\n' + + '#writing #microfiction #vss #marmalade', + cw: null, + visibility: 'public', + renoteCount: 0, + repliesCount: 0, + reactions: {}, + reactionEmojis: [], + emojis: [], + tags: ['micro365', 'writing', 'microfiction', 'vss', 'marmalade'], + fileIds: [], + files: [], + replyId: null, + renoteId: null } +} -export default async function insertPublished(res, word){ - const childLogger = logger.child({res,word}) - childLogger.trace("insertPublished called") +export default async function insertPublished(res, word) { + logger.trace("insertPublished called") + try { return db('published') - .insert({ + .insert({ id: res.createdNote.id, word, date: isoDate(sampleRes.createdNote.createdAt) - }) - .then(res => { - childLogger.trace("succesful insertion into table: published") - return res - }) - .catch(error => {throw error}) + }) + } catch (error) { + logger.trace("succesful insertion into table: published") + throw error + } + } //console.log(await insertPublished(sampleRes,'marmalade')) \ No newline at end of file diff --git a/database-calls/todaysPromptAlreadyPublished.mjs b/database-calls/todaysPromptAlreadyPublished.mjs index 22e0063..d1942e1 100644 --- a/database-calls/todaysPromptAlreadyPublished.mjs +++ b/database-calls/todaysPromptAlreadyPublished.mjs @@ -1,11 +1,17 @@ import { db } from "./db.mjs"; import { isoDate } from "../utilities.mjs"; +import logger from "../logger.mjs"; export default async function todaysPromptAlreadyPublished() { - const number = await db('published') + try { + const number = await db('published') .count('* as count') .where('date', isoDate()) - .catch(error=>{throw error}) - return number[0].count > 0 + return number[0].count > 0 + } catch (error) { + logger.error("todaysPromptAlreadyPublished failed!") + throw error + } + } //console.log(await todaysPromptAlreadyPublished())