ejs icon indicating copy to clipboard operation
ejs copied to clipboard

Trapping an ejs render error

Open Daxcor69 opened this issue 3 years ago • 1 comments

My application using express, crashes with the ejs render error data like it should. However I need to trap the error and handle it gracefully as I move into production. Now, the app crashes in the docker image, but kubernetes thinks it is still alive. I would like to show the user a "ooops" error page, instead of the render error and crash. I have posted in Stackoverflow but no response. I am looking on how to get kubernetes to see that it has crashed and restart.

Thanks Brad

Daxcor69 avatar Mar 17 '21 17:03 Daxcor69

Brad, why don't you try ensuring all the data to be render ejs are present using a wrapper /validation function to check if none of the object property are null || undefined , if the condition is true the render the page but if not create a custom route to render the error page .


function validateRenderObject(data){
   for(let prop in data){
     if(typeof prop == undefined || typeof prop==null){
        return false;
     }
     return true;
    }
}
app.get('/index',(req,res,next)=>{
    if(validateRenderObject(data)){
      res.render('page',data)
    }else{
      res.redirect(500,'/error')
     }
})
app.get('/error',(req,res,next)=>{
    res.render('error',obj);
})

caleberi avatar May 01 '21 12:05 caleberi

Leave a comment

ProxyLoader avatar Dec 15 '22 20:12 ProxyLoader

No action to take here, closing the issue.

mde avatar Dec 16 '22 04:12 mde