@ -76,7 +76,8 @@ var TextSecureServer = (function() {
}
}
if ( options . user && options . password ) {
if ( options . user && options . password ) {
fetchOptions . headers [ "Authorization" ] = "Basic " + btoa ( getString ( options . user ) + ":" + getString ( options . password ) ) ;
fetchOptions . headers [ "Authorization" ] =
"Basic " + btoa ( getString ( options . user ) + ":" + getString ( options . password ) ) ;
}
}
if ( options . contentType ) {
if ( options . contentType ) {
fetchOptions . headers [ "Content-Type" ] = options . contentType ;
fetchOptions . headers [ "Content-Type" ] = options . contentType ;
@ -93,13 +94,21 @@ var TextSecureServer = (function() {
}
}
return resultPromise . then ( function ( result ) {
return resultPromise . then ( function ( result ) {
if ( options . responseType === 'arraybuffer' ) {
if ( options . responseType === 'arraybuffer' ) {
result = result . buffer . slice ( result . byteOffset , result . byteOffset + result . byteLength ) ;
result = result . buffer . slice (
result . byteOffset ,
result . byteOffset + result . byteLength
) ;
}
}
if ( options . responseType === 'json' ) {
if ( options . responseType === 'json' ) {
if ( options . validateResponse ) {
if ( options . validateResponse ) {
if ( ! validateResponse ( result , options . validateResponse ) ) {
if ( ! validateResponse ( result , options . validateResponse ) ) {
console . log ( options . type , url , response . status , 'Error' ) ;
console . log ( options . type , url , response . status , 'Error' ) ;
reject ( HTTPError ( response . status , result , options . stack ) ) ;
reject ( HTTPError (
'promise_ajax: invalid response' ,
response . status ,
result ,
options . stack
) ) ;
}
}
}
}
}
}
@ -108,13 +117,18 @@ var TextSecureServer = (function() {
resolve ( result , response . status ) ;
resolve ( result , response . status ) ;
} else {
} else {
console . log ( options . type , url , response . status , 'Error' ) ;
console . log ( options . type , url , response . status , 'Error' ) ;
reject ( HTTPError ( response . status , result , options . stack ) ) ;
reject ( HTTPError (
'promise_ajax: error response' ,
response . status ,
result ,
options . stack
) ) ;
}
}
} ) ;
} ) ;
} ) . catch ( function ( e ) {
} ) . catch ( function ( e ) {
console . log ( options . type , url , 0 , 'Error' ) ;
console . log ( options . type , url , 0 , 'Error' ) ;
console . log ( e ) ;
var stack = e . stack + '\nInitial stack:\n' + options . stack ;
reject ( HTTPError ( 0 , e . toString ( ) , options . stack ) ) ;
reject ( HTTPError ( 'promise_ajax catch' , 0 , e . toString ( ) , stack ) ) ;
} ) ;
} ) ;
} ) ;
} ) ;
}
}
@ -141,14 +155,14 @@ var TextSecureServer = (function() {
return retry _ajax ( url , options ) ;
return retry _ajax ( url , options ) ;
}
}
function HTTPError ( code, response , stack ) {
function HTTPError ( message, code, response , stack ) {
if ( code > 999 || code < 100 ) {
if ( code > 999 || code < 100 ) {
code = - 1 ;
code = - 1 ;
}
}
var e = new Error ( ) ;
var e = new Error ( message + '; code: ' + code ) ;
e . name = 'HTTPError' ;
e . name = 'HTTPError' ;
e . code = code ;
e . code = code ;
e . stack = stack ;
e . stack + = '\nOriginal stack:\n' + stack ;
if ( response ) {
if ( response ) {
e . response = response ;
e . response = response ;
}
}