@@ -282,13 +282,10 @@ HttpPushRgb.prototype = {
282282 var url = this . switch . status . url ;
283283
284284 this . _httpRequest ( url , '' , 'GET' , function ( error , response , responseBody ) {
285- if ( error ) {
286- this . log ( 'getPowerState() failed: %s' , error . message ) ;
287- callback ( error ) ;
288- } else {
289- var powerOn = this . switch . status . bodyRegEx . test ( responseBody )
290- this . log ( 'power is currently %s' , powerOn ? 'ON' : 'OFF' ) ;
291- callback ( null , powerOn ) ;
285+ if ( ! this . _handleHttpErrorResponse ( 'getPowerState()' , error , response , responseBody , callback ) ) {
286+ var powerOn = this . switch . status . bodyRegEx . test ( responseBody )
287+ this . log ( 'power is currently %s' , powerOn ? 'ON' : 'OFF' ) ;
288+ callback ( null , powerOn ) ;
292289 }
293290 } . bind ( this ) ) ;
294291 } ,
@@ -325,10 +322,7 @@ HttpPushRgb.prototype = {
325322 }
326323
327324 this . _httpRequest ( url , body , this . http_method , function ( error , response , responseBody ) {
328- if ( error ) {
329- this . log ( 'setPowerState() failed: %s' , error . message ) ;
330- callback ( error ) ;
331- } else {
325+ if ( ! this . _handleHttpErrorResponse ( 'setPowerState()' , error , response , responseBody , callback ) ) {
332326 this . log ( 'setPowerState() successfully set to %s' , state ? 'ON' : 'OFF' ) ;
333327 callback ( undefined , responseBody ) ;
334328 }
@@ -349,10 +343,7 @@ HttpPushRgb.prototype = {
349343
350344 if ( this . brightness ) {
351345 this . _httpRequest ( this . brightness . status , '' , 'GET' , function ( error , response , responseBody ) {
352- if ( error ) {
353- this . log ( 'getBrightness() failed: %s' , error . message ) ;
354- callback ( error ) ;
355- } else {
346+ if ( ! this . _handleHttpErrorResponse ( 'getBrightness()' , error , response , responseBody , callback ) ) {
356347 var level = parseInt ( responseBody ) ;
357348 this . log ( 'brightness is currently at %s %' , level ) ;
358349 callback ( null , level ) ;
@@ -381,10 +372,7 @@ HttpPushRgb.prototype = {
381372 var url = this . brightness . set_url . replace ( '%s' , level ) ;
382373
383374 this . _httpRequest ( url , '' , this . brightness . http_method , function ( error , response , body ) {
384- if ( error ) {
385- this . log ( 'setBrightness() failed: %s' , error ) ;
386- callback ( error ) ;
387- } else {
375+ if ( ! this . _handleHttpErrorResponse ( 'setBrightness()' , error , response , responseBody , callback ) ) {
388376 this . log ( 'setBrightness() successfully set to %s %' , level ) ;
389377 callback ( ) ;
390378 }
@@ -408,10 +396,7 @@ HttpPushRgb.prototype = {
408396 var url = this . color . status ;
409397
410398 this . _httpRequest ( url , '' , 'GET' , function ( error , response , responseBody ) {
411- if ( error ) {
412- this . log ( '... getHue() failed: %s' , error . message ) ;
413- callback ( error ) ;
414- } else {
399+ if ( ! this . _handleHttpErrorResponse ( 'getHue()' , error , response , responseBody , callback ) ) {
415400 var rgb = responseBody ;
416401 var levels = this . _rgbToHsl (
417402 parseInt ( rgb . substr ( 0 , 2 ) , 16 ) ,
@@ -463,10 +448,7 @@ HttpPushRgb.prototype = {
463448 var url = this . color . status ;
464449
465450 this . _httpRequest ( url , '' , 'GET' , function ( error , response , responseBody ) {
466- if ( error ) {
467- this . log ( '... getSaturation() failed: %s' , error . message ) ;
468- callback ( error ) ;
469- } else {
451+ if ( ! this . _handleHttpErrorResponse ( 'getSaturation()' , error , response , responseBody , callback ) ) {
470452 var rgb = responseBody ;
471453 var levels = this . _rgbToHsl (
472454 parseInt ( rgb . substr ( 0 , 2 ) , 16 ) ,
@@ -522,10 +504,7 @@ HttpPushRgb.prototype = {
522504 this . log ( '_setRGB converting H:%s S:%s B:%s to RGB:%s ...' , this . cache . hue , this . cache . saturation , this . cache . brightness , r + g + b ) ;
523505
524506 this . _httpRequest ( url , '' , this . color . http_method , function ( error , response , body ) {
525- if ( error ) {
526- this . log ( '... _setRGB() failed: %s' , error ) ;
527- callback ( error ) ;
528- } else {
507+ if ( ! this . _handleHttpErrorResponse ( '_setRGB()' , error , response , responseBody , callback ) ) {
529508 this . log ( '... _setRGB() successfully set to #%s' , r + g + b ) ;
530509 callback ( ) ;
531510 }
@@ -559,6 +538,25 @@ HttpPushRgb.prototype = {
559538 } ) ;
560539 } ,
561540
541+ /**
542+ * Verify if response code equals '200', otherwise log error and callback
543+ * with a new Error object.
544+ * @param {String } functionStr Description used to create log and error message.
545+ * @param {Object } error Received error from client.
546+ * @param {Object } response Received reponse from client.
547+ * @param {Function } callback Reply function to call when error ocurred.
548+ * @return {Boolean } true: Error occurred, false otherwise
549+ */
550+ _handleHttpErrorResponse : function ( functionStr , error , response , responseBody , callback ) {
551+ if ( error ) {
552+ this . log ( functionStr + ' failed: %s' , error . message ) ;
553+ callback ( error ) ;
554+ } else if ( response . statusCode != 200 ) {
555+ this . log ( functionStr + ' returned HTTP error code: %s: "%s"' , response . statusCode , responseBody ) ;
556+ callback ( new Error ( "Received HTTP error code " + response . statusCode + ': "' + responseBody + '"' ) ) ;
557+ }
558+ } ,
559+
562560 /**
563561 * Converts an HSV color value to RGB. Conversion formula
564562 * adapted from http://stackoverflow.com/a/17243070/2061684
0 commit comments