@@ -158,7 +158,7 @@ AmazonMwsResource.prototype = {
158158 data . push ( value ) ;
159159 } )
160160 . on ( 'end' , function ( ) {
161- debug ( 'response after parsing tab delimited file %o ' , data ) ;
161+ debug ( 'response after parsing file %o ' , data ) ;
162162 var items = { } ;
163163 items . data = data ;
164164 items . Headers = {
@@ -170,45 +170,39 @@ AmazonMwsResource.prototype = {
170170 return callback ( null , items ) ;
171171 } )
172172 . on ( 'error' , function ( error ) {
173+ debug ( 'error ' , error ) ;
173174 return callback ( error ) ;
174175 } ) ;
175176 }
176177
177178 function processResponseType ( res , responseString , callback ) {
178- //debug('response %o ', response );
179+ //debug('res %o ', res );
179180 //debug('res.headers %o ', res.headers);
180181 if ( RESPONSE_CONTENT_TYPE . indexOf ( res . headers [ 'content-type' ] . toLowerCase ( ) ) > - 1 ) {
181182 debug ( 'It is XML Response' ) ;
182183 var items = xml2json . toJson ( responseString ) ;
183- //debug('response after parsing JSON %o ', response );
184+ //debug('res after parsing JSON %o ', res );
184185 items = JSON . parse ( items ) ;
185186 items . Headers = {
186187 'x-mws-quota-max' : res . headers [ 'x-mws-quota-max' ] || 'unknown' ,
187188 'x-mws-quota-remaining' : res . headers [ 'x-mws-quota-remaining' ] || 'unknown' ,
188189 'x-mws-quota-resetson' : res . headers [ 'x-mws-quota-resetson' ] || 'unknown' ,
189190 'x-mws-timestamp' : res . headers [ 'x-mws-timestamp' ]
190191 } ;
191- //debug('after adding header response ', response );
192+ //debug('after adding header res ', res );
192193 return callback ( null , items ) ;
193194 } else {
194195 debug ( 'It is NON-XML Response' ) ;
195196 var TAB_DELIMITER = '\t' ;
196197 var COMMA_DELIMITER = ',' ;
197- try {
198- parseCSVFile ( res , responseString , TAB_DELIMITER , callback ) ;
199- } catch ( Exception ) {
200- debug ( 'It is TAB_DELIMITER Exception ' , Exception ) ;
201- debug ( 'Let us try to delimit using COMMA_DELIMITER' ) ;
202- try {
203- parseCSVFile ( res , responseString , COMMA_DELIMITER , callback ) ;
204- } catch ( Exception ) {
205- debug ( 'It is COMMA_DELIMITER Exception ' , Exception ) ;
206- debug ( 'Parsing using both TAB_DELIMITER & COMMA_DELIMITER Failed.' ) ;
207- debug ( 'It may due to empty response or invalid request.' ) ;
208- return callback ( new Error ( 'Parsing fail.' ) ) ;
198+ parseCSVFile ( res , responseString , TAB_DELIMITER , function ( error , response ) {
199+ if ( ! _ . isEmpty ( error ) ) {
200+ debug ( 'It is TAB_DELIMITER failure.' ) ;
201+ debug ( 'Let us try to delimit using COMMA_DELIMITER' ) ;
202+ return parseCSVFile ( res , responseString , COMMA_DELIMITER , callback ) ;
209203 }
210- }
211-
204+ return callback ( null , response ) ;
205+ } ) ;
212206 }
213207 }
214208
0 commit comments