@ -27,7 +27,7 @@ public enum OnionRequestAPI {
// MARK: E r r o r
// MARK: E r r o r
public enum Error : LocalizedError {
public enum Error : LocalizedError {
case httpRequestFailedAt TargetSnode ( statusCode : UInt , json : JSON )
case httpRequestFailedAt Destination ( statusCode : UInt , json : JSON )
case insufficientSnodes
case insufficientSnodes
case invalidURL
case invalidURL
case missingSnodeVersion
case missingSnodeVersion
@ -37,7 +37,7 @@ public enum OnionRequestAPI {
public var errorDescription : String ? {
public var errorDescription : String ? {
switch self {
switch self {
case . httpRequestFailedAt TargetSnode ( let statusCode ) : return " HTTP request failed at target snode with status code: \( statusCode ) . "
case . httpRequestFailedAt Destination ( let statusCode ) : return " HTTP request failed at destination with status code: \( statusCode ) . "
case . insufficientSnodes : return " Couldn't find enough snodes to build a path. "
case . insufficientSnodes : return " Couldn't find enough snodes to build a path. "
case . invalidURL : return " Invalid URL "
case . invalidURL : return " Invalid URL "
case . missingSnodeVersion : return " Missing snode version. "
case . missingSnodeVersion : return " Missing snode version. "
@ -226,7 +226,7 @@ public enum OnionRequestAPI {
internal static func sendOnionRequest ( to snode : Snode , invoking method : Snode . Method , with parameters : JSON , associatedWith publicKey : String ) -> Promise < JSON > {
internal static func sendOnionRequest ( to snode : Snode , invoking method : Snode . Method , with parameters : JSON , associatedWith publicKey : String ) -> Promise < JSON > {
let payload : JSON = [ " method " : method . rawValue , " params " : parameters ]
let payload : JSON = [ " method " : method . rawValue , " params " : parameters ]
return sendOnionRequest ( with : payload , to : Destination . snode ( snode ) ) . recover2 { error -> Promise < JSON > in
return sendOnionRequest ( with : payload , to : Destination . snode ( snode ) ) . recover2 { error -> Promise < JSON > in
guard case OnionRequestAPI . Error . httpRequestFailedAt TargetSnode ( let statusCode , let json ) = error else { throw error }
guard case OnionRequestAPI . Error . httpRequestFailedAt Destination ( let statusCode , let json ) = error else { throw error }
throw SnodeAPI . handleError ( withStatusCode : statusCode , json : json , forSnode : snode , associatedWith : publicKey ) ? ? error
throw SnodeAPI . handleError ( withStatusCode : statusCode , json : json , forSnode : snode , associatedWith : publicKey ) ? ? error
}
}
}
}
@ -322,10 +322,10 @@ public enum OnionRequestAPI {
let b = try JSONSerialization . jsonObject ( with : bodyAsData , options : [ . fragmentsAllowed ] ) as ? JSON else { return seal . reject ( HTTP . Error . invalidJSON ) }
let b = try JSONSerialization . jsonObject ( with : bodyAsData , options : [ . fragmentsAllowed ] ) as ? JSON else { return seal . reject ( HTTP . Error . invalidJSON ) }
body = b
body = b
}
}
guard 200. . . 299 ~= statusCode else { return seal . reject ( Error . httpRequestFailedAt TargetSnode ( statusCode : UInt ( statusCode ) , json : body ) ) }
guard 200. . . 299 ~= statusCode else { return seal . reject ( Error . httpRequestFailedAt Destination ( statusCode : UInt ( statusCode ) , json : body ) ) }
seal . fulfill ( body )
seal . fulfill ( body )
} else {
} else {
guard 200. . . 299 ~= statusCode else { return seal . reject ( Error . httpRequestFailedAt TargetSnode ( statusCode : UInt ( statusCode ) , json : json ) ) }
guard 200. . . 299 ~= statusCode else { return seal . reject ( Error . httpRequestFailedAt Destination ( statusCode : UInt ( statusCode ) , json : json ) ) }
seal . fulfill ( json )
seal . fulfill ( json )
}
}
} catch {
} catch {