error handling

This commit is contained in:
Andrzej Stepien 2023-08-11 16:21:24 +02:00
parent 233ffc79ea
commit bf6fc14abe
9 changed files with 87 additions and 48 deletions

View File

@ -35,6 +35,9 @@ export default async function createNote(text) {
.then(data => {
return data
})
.catch(error =>{
throw error
})
}

View File

@ -2,17 +2,32 @@ import getNewPrompt from "./getNewPrompt.mjs"
import createNote from "./createNote.mjs"
import makeText from "./makeText.mjs"
import insertPublished from "./insertPublished.mjs"
import logger from "./logger.mjs"
const maxCount = 30000000
const minCount = 200000
export default async function daily () {
try {
const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
try {
const text = makeText(prompt)
try {
const note = await createNote(text)
insertPublished(note, prompt.word)
try {
await insertPublished(note, prompt.word)
} catch (error) {
logger.error(error, 'insertPublished failed!')
}
} catch (error) {
logger.error(error, 'createNote failed!')
}
} catch (error) {
logger.error(error, 'makeText failed!')
}
} catch (error) {
logger.error(error,'getNewPrompt failed!')
}
}

View File

@ -23,9 +23,7 @@ export default async function getNewPrompt({ minCount = 200000, maxCount = 30000
.whereRaw('length(word) > 3')
.whereNotNull('pronunciation')
.orderByRaw('count desc')
.catch(error=>{return error})
if(prompts instanceof Error){return prompts}
.catch(error=>{throw error})
const getBiasedRng = (min, max, bias, influence) => {
const random = Math.random() * (max - min) + min

View File

@ -1,5 +1,6 @@
import express from "express";
import bodyParser from "body-parser";
import logger from "./logger.mjs";
const app = express()
const port = 4000
app.use(bodyParser.json())
@ -12,3 +13,19 @@ app.post('/api', (req,res) => {
app.listen(port, () => {
console.log(`listening on port ${port}`)
})
process.on('uncaughtException', (err) => {
// log the exception
logger.fatal(err, 'uncaught exception detected');
// shutdown the server gracefully
server.close(() => {
process.exit(1); // then exit
});
// If a graceful shutdown is not achieved after 1 second,
// shut down the process completely
setTimeout(() => {
process.abort(); // exit immediately and generate a core dump file
}, 1000).unref()
process.exit(1);
});

View File

@ -57,8 +57,6 @@ export default async function insertPublished(res, word){
.then(res => {
return res
})
.catch(error => {
return error
})
.catch(error => {throw error})
}
//console.log(await insertPublished(sampleRes,'marmalade'))

View File

@ -1,6 +1,13 @@
import pino from 'pino'
import path from 'path'
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// const fileTransport = pino.transport({
// target: 'pino/file',
// options: { destination: `${__dirname}/app.log` },
// });
export default pino({
level: process.env.PINO_LOG_LEVEL || 'info',

View File

@ -15,5 +15,5 @@ const delay = ms => new Promise(res=>{setTimeout(res,ms)})
do{
await delay(1000)
await spam()
logger.info("Spam!")
logger.trace("Spam!")
}while(true)

View File

@ -7,7 +7,8 @@ export default async function todaysPromptAlreadyPublished() {
const number = await db('published')
.count('* as count')
.where('date', isoDate())
.catch(error=>{throw error})
return number[0].count > 0
}
console.log(await todaysPromptAlreadyPublished())
//console.log(await todaysPromptAlreadyPublished())