mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-17 14:08:26 +00:00
Make custom error messages actually work
This commit is contained in:
parent
d8932416f1
commit
c8cf5ebfa0
@ -15,77 +15,66 @@ import org.session.libsignal.utilities.logging.Log
|
|||||||
|
|
||||||
object FileServerAPIV2 {
|
object FileServerAPIV2 {
|
||||||
|
|
||||||
const val DEFAULT_SERVER = "http://88.99.175.227"
|
|
||||||
private const val DEFAULT_SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69"
|
private const val DEFAULT_SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69"
|
||||||
|
const val DEFAULT_SERVER = "http://88.99.175.227"
|
||||||
|
|
||||||
sealed class Error : Exception() {
|
sealed class Error(message: String) : Exception(message) {
|
||||||
object PARSING_FAILED : Error()
|
object ParsingFailed : Error("Invalid response.")
|
||||||
object INVALID_URL : Error()
|
object InvalidURL : Error("Invalid URL.")
|
||||||
|
|
||||||
fun errorDescription() = when (this) {
|
|
||||||
PARSING_FAILED -> "Invalid response."
|
|
||||||
INVALID_URL -> "Invalid URL."
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class Request(
|
data class Request(
|
||||||
val verb: HTTP.Verb,
|
val verb: HTTP.Verb,
|
||||||
val endpoint: String,
|
val endpoint: String,
|
||||||
val queryParameters: Map<String, String> = mapOf(),
|
val queryParameters: Map<String, String> = mapOf(),
|
||||||
val parameters: Any? = null,
|
val parameters: Any? = null,
|
||||||
val headers: Map<String, String> = mapOf(),
|
val headers: Map<String, String> = mapOf(),
|
||||||
// Always `true` under normal circumstances. You might want to disable
|
/**
|
||||||
// this when running over Lokinet.
|
* Always `true` under normal circumstances. You might want to disable
|
||||||
val useOnionRouting: Boolean = true
|
* this when running over Lokinet.
|
||||||
|
*/
|
||||||
|
val useOnionRouting: Boolean = true
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun createBody(parameters: Any?): RequestBody? {
|
private fun createBody(parameters: Any?): RequestBody? {
|
||||||
if (parameters == null) return null
|
if (parameters == null) return null
|
||||||
|
|
||||||
val parametersAsJSON = JsonUtil.toJson(parameters)
|
val parametersAsJSON = JsonUtil.toJson(parameters)
|
||||||
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
|
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun send(request: Request): Promise<Map<*, *>, Exception> {
|
private fun send(request: Request): Promise<Map<*, *>, Exception> {
|
||||||
val parsed = HttpUrl.parse(DEFAULT_SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL)
|
val url = HttpUrl.parse(DEFAULT_SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL)
|
||||||
val urlBuilder = HttpUrl.Builder()
|
val urlBuilder = HttpUrl.Builder()
|
||||||
.scheme(parsed.scheme())
|
.scheme(url.scheme())
|
||||||
.host(parsed.host())
|
.host(url.host())
|
||||||
.port(parsed.port())
|
.port(url.port())
|
||||||
.addPathSegments(request.endpoint)
|
.addPathSegments(request.endpoint)
|
||||||
|
|
||||||
if (request.verb == HTTP.Verb.GET) {
|
if (request.verb == HTTP.Verb.GET) {
|
||||||
for ((key, value) in request.queryParameters) {
|
for ((key, value) in request.queryParameters) {
|
||||||
urlBuilder.addQueryParameter(key, value)
|
urlBuilder.addQueryParameter(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val requestBuilder = okhttp3.Request.Builder()
|
val requestBuilder = okhttp3.Request.Builder()
|
||||||
.url(urlBuilder.build())
|
.url(urlBuilder.build())
|
||||||
.headers(Headers.of(request.headers))
|
.headers(Headers.of(request.headers))
|
||||||
when (request.verb) {
|
when (request.verb) {
|
||||||
HTTP.Verb.GET -> requestBuilder.get()
|
HTTP.Verb.GET -> requestBuilder.get()
|
||||||
HTTP.Verb.PUT -> requestBuilder.put(createBody(request.parameters)!!)
|
HTTP.Verb.PUT -> requestBuilder.put(createBody(request.parameters)!!)
|
||||||
HTTP.Verb.POST -> requestBuilder.post(createBody(request.parameters)!!)
|
HTTP.Verb.POST -> requestBuilder.post(createBody(request.parameters)!!)
|
||||||
HTTP.Verb.DELETE -> requestBuilder.delete(createBody(request.parameters))
|
HTTP.Verb.DELETE -> requestBuilder.delete(createBody(request.parameters))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.useOnionRouting) {
|
if (request.useOnionRouting) {
|
||||||
return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), DEFAULT_SERVER, DEFAULT_SERVER_PUBLIC_KEY)
|
return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), DEFAULT_SERVER, DEFAULT_SERVER_PUBLIC_KEY).fail { e ->
|
||||||
.fail { e ->
|
Log.e("Loki", "File server request failed.", e)
|
||||||
Log.e("Loki", "FileServerV2 failed with error",e)
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return Promise.ofFail(IllegalStateException("It's currently not allowed to send non onion routed requests."))
|
return Promise.ofFail(IllegalStateException("It's currently not allowed to send non onion routed requests."))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// region Sending
|
|
||||||
fun upload(file: ByteArray): Promise<Long, Exception> {
|
fun upload(file: ByteArray): Promise<Long, Exception> {
|
||||||
val base64EncodedFile = Base64.encodeBytes(file)
|
val base64EncodedFile = Base64.encodeBytes(file)
|
||||||
val parameters = mapOf("file" to base64EncodedFile)
|
val parameters = mapOf( "file" to base64EncodedFile )
|
||||||
val request = Request(verb = HTTP.Verb.POST, endpoint = "files", parameters = parameters)
|
val request = Request(verb = HTTP.Verb.POST, endpoint = "files", parameters = parameters)
|
||||||
return send(request).map { json ->
|
return send(request).map { json ->
|
||||||
json["result"] as? Long ?: throw OpenGroupAPIV2.Error.ParsingFailed
|
json["result"] as? Long ?: throw OpenGroupAPIV2.Error.ParsingFailed
|
||||||
@ -95,9 +84,8 @@ object FileServerAPIV2 {
|
|||||||
fun download(file: Long): Promise<ByteArray, Exception> {
|
fun download(file: Long): Promise<ByteArray, Exception> {
|
||||||
val request = Request(verb = HTTP.Verb.GET, endpoint = "files/$file")
|
val request = Request(verb = HTTP.Verb.GET, endpoint = "files/$file")
|
||||||
return send(request).map { json ->
|
return send(request).map { json ->
|
||||||
val base64EncodedFile = json["result"] as? String ?: throw Error.PARSING_FAILED
|
val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed
|
||||||
Base64.decode(base64EncodedFile) ?: throw Error.PARSING_FAILED
|
Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -34,23 +34,13 @@ object OpenGroupAPIV2 {
|
|||||||
private const val DEFAULT_SERVER_PUBLIC_KEY = "a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f488aaa93da7991238"
|
private const val DEFAULT_SERVER_PUBLIC_KEY = "a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f488aaa93da7991238"
|
||||||
const val DEFAULT_SERVER = "http://116.203.70.33"
|
const val DEFAULT_SERVER = "http://116.203.70.33"
|
||||||
|
|
||||||
sealed class Error : Exception() {
|
sealed class Error(message: String) : Exception(message) {
|
||||||
object Generic : Error()
|
object Generic : Error("An error occurred.")
|
||||||
object ParsingFailed : Error()
|
object ParsingFailed : Error("Invalid response.")
|
||||||
object DecryptionFailed : Error()
|
object DecryptionFailed : Error("Couldn't decrypt response.")
|
||||||
object SigningFailed : Error()
|
object SigningFailed : Error("Couldn't sign message.")
|
||||||
object InvalidURL : Error()
|
object InvalidURL : Error("Invalid URL.")
|
||||||
object NoPublicKey : Error()
|
object NoPublicKey : Error("Couldn't find server public key.")
|
||||||
|
|
||||||
fun errorDescription() = when (this) {
|
|
||||||
Error.Generic -> "An error occurred."
|
|
||||||
Error.ParsingFailed -> "Invalid response."
|
|
||||||
Error.DecryptionFailed -> "Couldn't decrypt response."
|
|
||||||
Error.SigningFailed -> "Couldn't sign message."
|
|
||||||
Error.InvalidURL -> "Invalid URL."
|
|
||||||
Error.NoPublicKey -> "Couldn't find server public key."
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class DefaultGroup(val id: String, val name: String, val image: ByteArray?) {
|
data class DefaultGroup(val id: String, val name: String, val image: ByteArray?) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user