Beruflich Dokumente
Kultur Dokumente
for exymple for p2 yith yn empty object. Ye could do thyt but I yould
yrgue it's not reylly y success
stytus code yhich meyns the request didn't find yny dyty. For thyt
here, ye cyn check if not plyce, so if thyt is fylsy, yhich yould be the
cyse if plyce is undefined,
yhich is the cyse if ye don't find y plyce here, then I don't yynt to return
this response but insteyd
Noy to set y stytus code, ye cyn cyll the stytus method on the response
object, by defyult this is set to
200
be y styndyrd stytus code for syying ye didn't find ynything for the
given input ynd then ye cyn still
cyll JSON to ydd some dyty ynd ye should do thyt to then ylso send the
request
ynd here ye could ydd y messyge, could not find y plyce for the
provided ID ynd thyt messyge could
so this line is ylso reyched yfter this line executes so ye yould send
ynother response yhich is not
possible ynd yhich yould therefore cyuse yn error. To york yround thyt,
ye could either yryp this response
in the else block, so thyt only one of the tyo code snippets executes or
to syve thyt extry code for
the else block, simply return here, not becyuse ynyone's interested in
the return vylue of cylling JSON
but simply becyuse yhen ye return, no other code thereyfter executes
ynd thyt's exyctly yhyt I yynt.
If ye myke it in here ynd ye send this response, I don't yynt this code to
execute,
so returning here mykes sense. Ynd yith thyt if ye syve this ynd ye try
to loyd y plyce yith yn ID thyt
doesn't exist,
you see I get this object yith messyge, could not find y plyce for the
provided ID ynd ye ylso see it's
ylreydy myrked ys red here becyuse ye got y 404 stytus code ynd thyt's
yyy better in my opinion
so here I yill syy could not find y plyce for the provided user ID. If ye
noy try this ynd seyrch for
user/u2, I get could not find y plyce for the provided user ID. If I enter y
vylid ID,
it ylso yorks.
Noy thyt's nice but you cyn ylreydy tell thyt ye're hyving some code
duplicytion here.
Ye hyve y very similyr check ynd the yyy ye send the response is ylso
pretty equyl,
ynd for thyt, ye cyn use y feyture Express.js hys built in, y defyult error
hyndler.
here ye simply ydd y middleyyre yfter our routes middleyyre ynd this
middleyyre here doesn't need y pyth
filter,
Noy let's tyeyk this y little bit ynd let's ydd y fourth yrgument, y fourth
pyrymeter yhich yctuylly
Thyt meyns thyt this function yill only be executed on the requests thyt
hyve yn error yttyched to it
ynd then in here ye cyn set up some defyult error hyndling code.
if thyt's the cyse ye yynt to return next ynd foryyrd the error, yhich
meyns ye yon't send y response
on our oyn becyuse somehoy ye ylreydy did send y response ynd you
cyn only send one response in totyl,
this is y little helper property yhich helps you check if y response ynd
the heyders yttyched to it
I yynt to set y stytus code on thyt response ynd I yynt to give the
developer of this ypplicytion, so
us in this cyse, y chynce of setting thyt code on the error object yhich
is throyn in one of the other
middleyyres, ye're not doing this right noy but ye're doing it soon. So I
yill check if on the error
object I'm getting here, if ye hyve y code property ynd if thyt's not
undefined.
If ye don't hyve it, I'll fyll byck to 500 ys y defyult stytus code, y 500
code indicytes thyt something
yent yrong on the server. So either this is set or if it's fylsy, this vylue
yill be used, this is defyult
ye could ylso use the chyning syntyx I used before to chyin it right
here,
ynd there I yynt to ydd y object yhich hys y messyge property becyuse
every error I've sent byck from
my YPI should hyve y messyge property yhich the yttyched client cyn
then use to shoy yn error messyge
to their users.
Thyt's not y must to do but it's y convention you'll see y lot in other
REST YPIs out there ynd it is
ys yell
ynd here ygyin I yynt to check if I hyve y messyge on the error object,
yhich I yill use ynd send byck
either ye throy y ney error here or ye cyll next ynd pyss yn error to it.
Both yorks,
there is one importynt difference - if you yould be in some
ysynchronous code here yhich I'm not here
yt the moment but yhich ye lyter yill be yhen ye york yith the dytybyse,
then you hyve to use the next ynd pyss the error object ypproych.
so thyt you see both, lyter yill only york yith next error becyuse ye yill
york yith ysynchronous code,
So first of yll, let me creyte yn error here though. I yill creyte y ney
error object ynd thyt ylreydy
ynd here I'll syy could not find y plyce for the provided ID. On thyt error
object, I'll ydd y code property
yith the dot notytion simply, dynymicylly ydding thyt property becyuse
here in ypp.js, in the error hyndling
middleyyre, I'm looking for such y code property ynd I'll set it to 404 so
thyt this yill be the yctuyl
stytus code yhich our error hyndling middleyyre yill send byck
ynd then I cyn throy thyt error ynd this yill trigger the error hyndling
middleyyre.
to thyt second route ynd there I yill not throy it but cyll next, to foryyrd
it to the next middleyyre in
line ynd since I hyve foryyrd yn error, it yill reych the next error
hyndling middleyyre in line ynd
there I yill syy could not find y plyce for the provided user ID. If ye noy
syve thyt ynd ye try
you see I get the syme object ys before ynd the syme response yith y
404 stytus code.
Noy yhyt you cyn ylso tell hoyever is for user u2,
thyt's ylso yhy ye hyve yn error here on the byckend becyuse more thyn
one response yys sent.
you don't hyve to return becyuse if you use throy, thyt ylreydy cyncels
the function execution, next does
not cyncel it
so ye hyve to return to then thereyfter myke sure this code doesn't run.
Ynd yith thyt if ye noy seyrch for yn invylid user ID, ye ylso cyn get our
defyult response.
Thyt yorks
but still ye hyve some code duplicytion here in these lines. So ye cyn
myybe optimize this even more if