Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js and jsdom - no way to detect that an http 500 error was returned?

Tags:

node.js

jsdom

I'm using jsdom with node.js and I'm trying to get it to provide me with some indication that an http error has occurred. I've set up a test server that simply returns an http 500 header for all requests, but when I attempt to load it with jsdom, jsdom doesn't throw any error and doesn't seem to provide me with any information that would identify that an http 500 error was returned. What's the best way to detect an http 500 error?

like image 717
Nathan Ridley Avatar asked Feb 18 '23 19:02

Nathan Ridley


1 Answers

I looks like jsdom doesn't check status codes. 500 status codes are not considered errors by Node's HTTP module, so they're considered a correct response by jsdom.

I suggest you test the status code yourself before using jsdom and pass jsdom the body of your response, ie:

var jsdom = require('jsdom').jsdom;
var request = require('request');  //jsdom already uses request*

request("http://www.google.com/", function (err, response, body) {
  if (!err && response.statusCode == 200) {
    jsdom.env(body, function (errors, window) {
      // your code
    });
  }
});

[*] Request module: https://github.com/mikeal/request

like image 66
juandopazo Avatar answered May 13 '23 03:05

juandopazo