Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jason limmm
jason limmm
8,009 Points

error status code possibly not defined???

here is my code

const error = new Error('MIMIMIIIIIII *voicecrack');

//cut out lines of code

app.use((req, res, next)=>{
    res.status(error.status);
    next(error);
})

app.use((err, req, res, next)=>{
    res.render('error', error);
})

i checked the chrome development console and it said this:

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
    at ServerResponse.writeHead (node:_http_server:352:11)
    at ServerResponse._implicitHeader (node:_http_server:338:8)
    at write_ (node:_http_outgoing:945:9)
    at ServerResponse.end (node:_http_outgoing:1056:5)
    at ServerResponse.send (C:\Users\User\Desktop\life\node_modules\express\lib\response.js:232:10)
    at done (C:\Users\User\Desktop\life\node_modules\express\lib\response.js:1045:10)
    at exports.renderFile (C:\Users\User\Desktop\life\node_modules\pug\lib\index.js:448:12)
    at exports.__express [as engine] (C:\Users\User\Desktop\life\node_modules\pug\lib\index.js:493:11)
    at View.render (C:\Users\User\Desktop\life\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\User\Desktop\life\node_modules\express\lib\application.js:657:10)

and i am assuming that i didn't define the status code of the variable 'error' if that's the case, then i don't really know how to define it.

i tried to run the error handler line by line using the vs code debug feature but as of now i couldn't get my web server to load for some reason, maybe it is slow internet

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey jason limmm 👋

When you're creating a new Error object the status code property is not automatically being set. You'll have to explicit set it:

error.status = 500 

Of course you'll want to update that to use the appropriate status code for the type of error you're handling 🙂

jason limmm
jason limmm
8,009 Points
app.use((req, res, next)=>{
    error.status=500;
    res.status(error.status);
    next(error);
})

is it okay to put my error.status in inside like this