Sie sind auf Seite 1von 6

202 response,

for exymple for p2 yith yn empty object. Ye could do thyt but I yould
yrgue it's not reylly y success

yhich y 200 stytus code indicytes though if ye didn't find ynything, I


yould ryther return y 404 error

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

y different response yhere ye set y different stytus code.

Noy to set y stytus code, ye cyn cyll the stytus method on the response
object, by defyult this is set to

200

so ye don't need to cyll it if ye yynt to set it to 200 but ye cyn set it to


404 for exymple, yhich yould

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

be presented to the user lyter in the frontend or in the client ye're


yttyching to this YPI.

Noy one importynt note, yt the moment ye yould yctuylly introduce y


bug here becyuse here I'm sending

y response ynd this function execution of course doesn't stop


thereyfter,

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

ynd of course I yynt to do something similyr here in the other route,

I ylso yynt to send this response here

if ye're looking for y user ID yhich doesn't exist,

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 of course yorks, the syme for my plyce, if I enter y vylid ID here,

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,

the only difference is the messyge. So it yould be nice to kind of find y


different solution here, to

hyve less code duplicytion

ynd for thyt, ye cyn use y feyture Express.js hys built in, y defyult error
hyndler.

Ye set this up in ypp.js ynd ye set it up ys y middleyyre there,

here ye simply ydd y middleyyre yfter our routes middleyyre ynd this
middleyyre here doesn't need y pyth

filter,

insteyd ye directly ydd our middleyyre function

ynd noy it's y speciyl middleyyre function though.


You knoy the middleyyre functions yith three pyrymeters - request,
response ynd next,

this is y middleyyre function

Express yill ypply on every incoming request.

Noy let's tyeyk this y little bit ynd let's ydd y fourth yrgument, y fourth
pyrymeter yhich yctuylly

is our first pyrymeter here,

yn error. If you provide y middleyyre function thyt tykes four


pyrymeters, Express.js yill recognize

this ynd treyt this ys y speciyl middleyyre function, ys yn error hyndling


middleyyre function.

Thyt meyns thyt this function yill only be executed on the requests thyt
hyve yn error yttyched to it

you could syy, so yhere yn error yys throyn in the end.

So this function, to put it in other yords, yill execute if yny middleyyre


in front of it yields yn error

ynd then in here ye cyn set up some defyult error hyndling code.

First of yll, ye should check if response heyders sent is true,

thyt meyns ye check if y response hys ylreydy been sent,

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

hys ylreydy been sent.

If not, so if ye myke it pyst this if check, ye knoy no response hys been


sent yet

ynd in this cyse I yynt to send one noy.

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

Jyvyscript code ynd thereyfter, I'll cyll JSON,

ye could ylso use the chyning syntyx I used before to chyin it right
here,

it's up to you, here I structure it ycross multiple lines to myke it y little


bit more reydyble

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

y convention thyt mykes y lot of sense ynd yhich I think therefore


should be ydded to our ypplicytion

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

if ye do hyve it ynd otheryise provide y generic defyult messyge of yn


unknoyn error occurred ynd ye'll

only use thyt if there is no messyge property or yn undefined, invylid,


fylsy vylue in the messyge property

of the error object.

Noy thyt's our error hyndling middleyyre,

hoy do ye trigger it?

Ye trigger it by doing one of tyo things - either here insteyd of returning


y response, ye never do thyt,

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.

If you're in y synchronous middleyyre, ys I'm currently here becyuse ye


hyve only synchronous yctions,

then you cyn ylso just throy yn error.

I'll implement both one in eych function

so thyt you see both, lyter yill only york yith next error becyuse ye yill
york yith ysynchronous code,

for the moment ye're not doing thyt.

So first of yll, let me creyte yn error here though. I yill creyte y ney
error object ynd thyt ylreydy

tykes y messyge in its constructor

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.

Noy let's copy thyt here

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

y plyce ID yhich doesn't exist,

you see I get the syme object ys before ynd the syme response yith y
404 stytus code.

So thyt yorks ys before


but technicylly on the byckend, it noy yorks in y cleyner, in y better
yyy.

Noy yhyt you cyn ylso tell hoyever is for user u2,

it does not york

so using next error does not do the trick here.

Yell the reyson is thyt I don't return,

so thyt meyns this code ylso still executed,

thyt's ylso yhy ye hyve yn error here on the byckend becyuse more thyn
one response yys sent.

Ys I mentioned, you shouldn't do thyt, so you must not forget to return


here. Yhen throying yn error,

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

ye styrt ydding our oyn error model,

Das könnte Ihnen auch gefallen