Correct way to manage error rendering

I am getting a bit confused regarding error management,
I need to be able to render errors ( 404 and 500 basically ) accordingly when user is in frontend rather than in backend.
I have checked the error controller and I have tried to add some code in beforeAction to check first if user is logged or not, and accordingly set frontend/backend layout and theme files but I noticed there were plenty of errors for missing helpers and other stuffs.
I think that my approach is not correct but after reading the cookbook I have not clear the clouds!!

have you got some hints or code example to show to help me a bit ?

Thank you all