Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So, up until this point in the course
we haven't really handled errors
in a good way or in a central place in our application.
What we did was to simply send back
an error message as JSON in each route handler
in case something went wrong.
Right?
So that's basically what we're gonna fix in this section.
But for now, let's take a minute
to just get a brief overview of error handling in Express.
And we start by actually distinguishing
between two types of errors that can occur.
Operational errors and programming errors.
So, operational errors are problems
that we can predict will inevitably happen
at some point in the future.
And so we just need to handle them in advance.
They have nothing to do with bugs in our code.
Instead, they depend on the user, or the system,
or the network.
So, things like a user accessing an invalid route,
inputting invalid data, or an application failing to connect
to the database.
All these are operational errors
that we will need to handle in order
to prepare our application for these cases.
You will also see the term, "exception"
being used instead of error
and while they are conceptually different,
many people use the terms error
and exception interchangeably.
And I'm just gonna call them, "errors,"
in this course as well in order to avoid confusion.
Anyway, on the other hand, we have programming errors.
Which are simply bugs that we developers
introduce into our code.
Like, for example, trying to read properties
from an undefined variable, using await without async,
accidentally using request.query instead of request.body,
or many other errors, really, that we might make.
So, you know how it works, right?
And they are really inevitable
but also more difficult to find and to handle.
Okay, so, it's important that you understand
this crucial difference between operational errors
and programming errors.
So, when we're talking about error handling with Express,
we mainly just mean operational errors.
Because these are the ones that are easy
to catch and to handle with our Express application.
And Express actually comes with error handling
out of the box.
So, all we have to do is to write
a global express error handling middleware
which will then catch errors coming
from all over the application.
So, no matter if it's an error coming from a route handler,
or a model validator or really, someplace else,
the goal is that all these errors end up
in one central error handling middleware.
So that we can send a nice response back to the client
letting them know what happened.
And so, really, handling in this case just means
sending a response letting the user know what happened.
But handling can also mean, in other cases,
retrying the operation or crashing the server,
or just ignoring the error altogether.
Sometimes, that's just the best option.
Now the beauty of having a global error handling middleware
is that it allows for a nice separation of concerns.
So, we don't have to worry about error handling
right in our business logic or our controllers,
or really anywhere in our application.
We can simply send the errors down to the error handler
which will then decide what to do with them next.
All right?
So, with all that being said,
let's now actually start implementing this.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.