JedWatson/node-premailer — pull request #2 by bmoeskau (2014-06-26 16:24:24+00:00)
Title: Better Premailer server error handling

I've found that lately the Premailer server has been failing inconsistently, and the error message I was getting back from premailer-api was `SyntaxError: Unexpected token <` along with a stack trace. This was causing me to think I was passing invalid HTML as input. After debugging, I found out that in fact, this error is due to premailer-api's current error handling assuming the Premailer response is valid JSON. In fact, (unfortunately) the response I'm getting from Premailer looks like this currently:

Application Error

That is the `body` value being passed to `JSON.parse`, hence the unexpected token error. Obviously the error coming back from Premailer itself appears to be due to bad handling on their side, but I guess trying to deal with it in a sensible way is at least better than raising it as a misleading error from premailer-api.

The suggested patch is ugly, but at least allows for clean reporting of what's going on to the end user of the lib. Now, how to go about fixing the Premailer server....