mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-03-01 06:07:37 +00:00
Add headers to StatusCodeError.
This commit is contained in:
committed by
Greyson Parrelli
parent
3a1ed7e4ac
commit
b563d7e855
@@ -1466,7 +1466,7 @@ object BackupRepository {
|
||||
*/
|
||||
private fun initBackupAndFetchAuth(): NetworkResult<ArchiveServiceAccessPair> {
|
||||
return if (!RemoteConfig.messageBackups) {
|
||||
NetworkResult.StatusCodeError(555, null, null, NonSuccessfulResponseCodeException(555, "Backups disabled!"))
|
||||
NetworkResult.StatusCodeError(555, null, null, emptyMap(), NonSuccessfulResponseCodeException(555, "Backups disabled!"))
|
||||
} else if (SignalStore.backup.backupsInitialized) {
|
||||
getArchiveServiceAccessPair().runOnStatusCodeError(resetInitializedStateErrorAction)
|
||||
} else if (isPreRestoreDuringRegistration()) {
|
||||
|
||||
@@ -22,7 +22,7 @@ import kotlin.reflect.KClass
|
||||
import kotlin.reflect.cast
|
||||
import kotlin.time.Duration
|
||||
|
||||
typealias StatusCodeErrorAction = (NetworkResult.StatusCodeError<*>) -> Unit
|
||||
typealias StatusCodeErrorAction = (StatusCodeError<*>) -> Unit
|
||||
|
||||
/**
|
||||
* A helper class that wraps the result of a network request, turning common exceptions
|
||||
@@ -171,8 +171,8 @@ sealed class NetworkResult<T>(
|
||||
data class NetworkError<T>(val exception: IOException) : NetworkResult<T>()
|
||||
|
||||
/** Indicates we got a response, but it was a non-2xx response. */
|
||||
data class StatusCodeError<T>(val code: Int, val stringBody: String?, val binaryBody: ByteArray?, val exception: NonSuccessfulResponseCodeException) : NetworkResult<T>() {
|
||||
constructor(e: NonSuccessfulResponseCodeException) : this(e.code, e.stringBody, e.binaryBody, e)
|
||||
data class StatusCodeError<T>(val code: Int, val stringBody: String?, val binaryBody: ByteArray?, val headers: Map<String, String>, val exception: NonSuccessfulResponseCodeException) : NetworkResult<T>() {
|
||||
constructor(e: NonSuccessfulResponseCodeException) : this(e.code, e.stringBody, e.binaryBody, e.headers, e)
|
||||
|
||||
inline fun <reified T> parseJsonBody(): T? {
|
||||
return try {
|
||||
@@ -244,7 +244,7 @@ sealed class NetworkResult<T>(
|
||||
|
||||
is NetworkError -> NetworkError<R>(exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is ApplicationError -> ApplicationError<R>(throwable).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is StatusCodeError -> StatusCodeError<R>(code, stringBody, binaryBody, exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is StatusCodeError -> StatusCodeError<R>(code, stringBody, binaryBody, headers, exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ sealed class NetworkResult<T>(
|
||||
is Success -> result(this.result).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is NetworkError -> NetworkError<R>(exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is ApplicationError -> ApplicationError<R>(throwable).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is StatusCodeError -> StatusCodeError<R>(code, stringBody, binaryBody, exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
is StatusCodeError -> StatusCodeError<R>(code, stringBody, binaryBody, headers, exception).runOnStatusCodeError(statusCodeErrorActions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,7 +354,7 @@ sealed class NetworkResult<T>(
|
||||
}
|
||||
|
||||
private fun <T : Any> WebsocketResponse.toStatusCodeError(): NetworkResult<T> {
|
||||
return StatusCodeError(NonSuccessfulResponseCodeException(this.status, "", this.body))
|
||||
return StatusCodeError(NonSuccessfulResponseCodeException(this.status, "", this.body, this.headers))
|
||||
}
|
||||
|
||||
private fun <T : Any> WebsocketResponse.toSuccess(responseJsonClass: KClass<T>): NetworkResult<T> {
|
||||
|
||||
@@ -15,29 +15,36 @@ open class NonSuccessfulResponseCodeException : IOException {
|
||||
val code: Int
|
||||
val stringBody: String?
|
||||
val binaryBody: ByteArray?
|
||||
val headers: Map<String, String>
|
||||
|
||||
constructor(code: Int) : super("StatusCode: $code") {
|
||||
this.code = code
|
||||
this.stringBody = null
|
||||
this.binaryBody = null
|
||||
this.headers = emptyMap()
|
||||
}
|
||||
|
||||
constructor(code: Int, message: String) : super("[$code] $message") {
|
||||
this.code = code
|
||||
this.stringBody = null
|
||||
this.binaryBody = null
|
||||
this.headers = emptyMap()
|
||||
}
|
||||
|
||||
constructor(code: Int, message: String, body: String?) : super("[$code] $message") {
|
||||
@JvmOverloads
|
||||
constructor(code: Int, message: String, body: String?, headers: Map<String, String> = emptyMap()) : super("[$code] $message") {
|
||||
this.code = code
|
||||
this.stringBody = body
|
||||
this.binaryBody = null
|
||||
this.headers = headers.mapKeys { it.key.lowercase() }
|
||||
}
|
||||
|
||||
constructor(code: Int, message: String, body: ByteArray?) : super("[$code] $message") {
|
||||
@JvmOverloads
|
||||
constructor(code: Int, message: String, body: ByteArray?, headers: Map<String, String> = emptyMap()) : super("[$code] $message") {
|
||||
this.code = code
|
||||
this.stringBody = null
|
||||
this.binaryBody = body
|
||||
this.headers = headers.mapKeys { it.key.lowercase() }
|
||||
}
|
||||
|
||||
fun is4xx(): Boolean {
|
||||
|
||||
@@ -38,6 +38,10 @@ public class WebsocketResponse {
|
||||
return headers.get(Preconditions.checkNotNull(key.toLowerCase()));
|
||||
}
|
||||
|
||||
public Map<String, String> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public boolean isUnidentified() {
|
||||
return unidentified;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user