I want to load a new error page when a function throws an error.
I want to show an error page whenever an error is thrown but the page is stuck at a loading screen, and there are error messages on my log. When getStockPoints
throws an error and I export this function to app.js and call it there with try and catch block, then it catches the error but the page is just stuck at loading. I want it to go to an error page.
module.exports = {
async getStocks () {
return stocks
},
async getStockPoints (stockName, timestamp) {
if (Math.random() < FAILUE_RATE) {
throw new Error('Failed to generate stock data')
}
if (!stocks.includes(stockName)) {
throw new Error(`Uknown stock ${stockName}`)
}
const timestamps = getTimestamps( timestamp, 10)
return getSeries(stockName, timestamps).map((value, idx) => ({
value,
timestamp: timestamps[idx]
}))
}
}
I did the try-catch method and tried to load a new page but it does not work.
app.get('/stocks/:symbol', async (req, res) => {
const { params: { symbol } } = req
try{
const data = await stocks.getStockPoints(symbol, new Date())
res.render('stockPoints', {DATA: data})
}
catch(e){
console.log("ERRORs: ")
console.error(e)
}
})
The page is stuck at a loading screen. Here is the error that I am getting:
Error: Uknown stock df
at Object.getStockPoints (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\stocks.js:47:13)
at C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\app.js:24:31
at Layer.handle [as handle_request] (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\index.js:284:15
at param (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\index.js:365:14)
at param (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\index.js:376:14)
at Function.process_params (C:\Users\talha_znbd0fl\Desktop\JavaScript-Challenge-master-main\JavaScript-Challenge-master-main\node_modules\express\lib\router\index.js:421:3)
CodePudding user response:
You are stuck at a loading page because your server never responds to the request. There are multiple functions you can call to serve a result, but if you likely want to render an error page. To do this, all you have to do is call res.render
inside your exception handler. Just like this:
app.get('/stocks/:symbol', async (req, res) => {
const { params: { symbol } } = req
try{
const data = await stocks.getStockPoints(symbol, new Date())
res.render('stockPoints', {DATA: data})
} catch (e){
console.log("ERRORs: ")
console.error(e)
res.render('errorPage')
}
})
Then, create the template errorPage
. It can be anything, but here's something simple to get you started:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
<h1>An error occured</h1>
</body>
</html>