So before we add sessions, let's add error handling.
We actually got an error here,
thanks to the missing session set up,
but there also are other things that could go wrong.
For example,
if we have a look at D off the controller,
there I'm calling user signup
and that signup method in my model,
that of course could also fail because there,
I'm hashing the password, this could fail,
and I'm trying to insert a user. This could also fail.
For example,
if we temporarily lost the database connection,
and therefore we wanna handle such errors.
Now, thankfully express gives us a default error
handling mechanism
In app JS,
We can register a special middleware.
Typically we do that at the very end here,
which is a error handling middleware.
We could do this here,
but since I already have a middlewares folder,
I'll add it in there.
Error dash handler JS sounds like a fitting file name.
In there, I'll trade a function because
all middlewares are functions
and this function will receive a name of
handle errors
like this.
But then this is a special middleware function because it
does not just receive free parameter values,
request, response and next, but it actually receives four.
It receives the special error parameter
value as a first value
and expressible call this function
whenever we have an error in one of the other middlewares or
route handler functions,
and then this first parameter value will be an error objects
with more details about the error that occured.
Now it's in here where I now wanna handle the case that we
have an error on one of our pages for this in here,
we can console lock the error so that we have some log on
the server. This will not be visible to our users.
And then
I wanna render
a template
a error page,
which I want to show in case of such errors.
And I don't just want to render it.
I also want to set a specific status code in such cases,
and I'll set the status code to 500,
which indicates a service site error.
We also have oper errors like a not found error,
but these errors will be handled in different places.
This error handling middleware with this line of code should
become active,
whenever something went wrong on the server and we're not
able to recover from it.
So in this case,
I'll set the status code off the response to 500.
So our response will be sent,
but it will indicate that something went wrong.
And I want to render an error page.
At moment,
we have no error page and therefore back in my views folder,
I'll add a third sub folder next to admin and customer,
which will name base because in there I want to have some
general views, which are not admin or customer specific,
or actually let's name it shared to make this even clearer.
And in this shared folder, I'll add a 500 EJS file,
which will be my template for 500 errors.
Now in there I'll then copy the structure from
the signup page
like this.
But of course, I don't want to have any form in there.
I'll get rid of that form here.
I just want to have the include of the head part,
change the page title to an error
occurred.
Don't import forums and off CSS.
I don't need that here include the header though.
And then here in the H one element,
I'll just say something went wrong
and I'll add a paragraph
where I say, unfortunately, something went wrong.
Please try again later.
I'll also add a second paragraph with an anchor tag below it
which will lead back to the starting page and which I'll
give a clause of BTN.
So here I'm using my button styles again for this anchor tag
now, and as a caption for this anchor tag,
I'll say back to safety.
So that's now my default error template,
which should be rendered if I have a server site error and
therefore back in the era handler JS file,
I want to render this. So I want to render shared slash 500.
That's the path to this specific template here
and actually
here in this 500 EJS file.
My includes pops here won't work because they're,
I'm trying to go up one level and then into an includes
folder, but that doesn't exist here.
Instead here,
I would now have to go into the customer or admin includes
folder based on which role I have and to make this a bit
easier.
And because we'll share head header and footer between
customer and admin, anyways,
I will actually grab the includes folder in the customer
folder and bring that over into the shared folder.
I'll move it.
So then the pops here and 500 EJS are correct again.
And now in the customer folder,
I'll have to update the puffs in the sign up
and log in EJS files.
Here we now need to go up one level
and then another level.
So out of,
off into customer and then interviews and then dive into
shared and then into includes.
And this segment now needs to be added to all the include
pops I have here
like this,
and also in log in,
EJS there, by the way,
I will already change the page title to log in to fix this
as well, update the include path here, and then also here.
And with that, that should be working.
Now I of course want to make sure that my error handling
middleware is exposed and available outset of this error
handler JS file as well. So I will export it like this.
And then in app JS here, we can
import this.
We can add the error handler middleware
by requiring dot slash
middlewares slash error handler.
And then at the very bottom, as mentioned before,
I want to use this error handler middleware,
if I'll now reload this page,
I get a different error because I have an error in one of my
pops here, as it seems,
in 500 EJS
yeah, here,
I'm still going up one level to then dive into includes.
This is not wrong.
Includes is next to 500 EJS so it should be just includes
head includes header and includes footer in the 500 EJS file
with that. If I reload, we now get this error page,
which is handling that CSR F error,
which of course is an error we'll still have to fix,
but we can tell that error handling is working here and that
is an important first step.