@@ -22,34 +22,29 @@ import org.matrix.android.sdk.api.session.contentscanner.ContentScannerError
2222import org.matrix.android.sdk.api.session.contentscanner.ScanFailure
2323import org.matrix.android.sdk.internal.di.MoshiProvider
2424import java.io.IOException
25+ import java.net.UnknownHostException
2526import javax.net.ssl.HttpsURLConnection
2627
27- fun Throwable.is401 () =
28- this is Failure .ServerError &&
29- httpCode == HttpsURLConnection .HTTP_UNAUTHORIZED && /* 401 */
30- error.code == MatrixError .M_UNAUTHORIZED
31-
32- fun Throwable.is404 () =
33- this is Failure .ServerError &&
34- httpCode == HttpsURLConnection .HTTP_NOT_FOUND && /* 404 */
35- error.code == MatrixError .M_NOT_FOUND
36-
37- fun Throwable.isTokenError () =
38- this is Failure .ServerError &&
39- (error.code == MatrixError .M_UNKNOWN_TOKEN ||
40- error.code == MatrixError .M_MISSING_TOKEN ||
41- error.code == MatrixError .ORG_MATRIX_EXPIRED_ACCOUNT )
42-
43- fun Throwable.isLimitExceededError () =
44- this is Failure .ServerError &&
45- httpCode == 429 &&
46- error.code == MatrixError .M_LIMIT_EXCEEDED
47-
48- fun Throwable.shouldBeRetried (): Boolean {
49- return this is Failure .NetworkConnection ||
50- this is IOException ||
51- this .isLimitExceededError()
52- }
28+ fun Throwable.is401 () = this is Failure .ServerError &&
29+ httpCode == HttpsURLConnection .HTTP_UNAUTHORIZED && /* 401 */
30+ error.code == MatrixError .M_UNAUTHORIZED
31+
32+ fun Throwable.is404 () = this is Failure .ServerError &&
33+ httpCode == HttpsURLConnection .HTTP_NOT_FOUND && /* 404 */
34+ error.code == MatrixError .M_NOT_FOUND
35+
36+ fun Throwable.isTokenError () = this is Failure .ServerError &&
37+ (error.code == MatrixError .M_UNKNOWN_TOKEN ||
38+ error.code == MatrixError .M_MISSING_TOKEN ||
39+ error.code == MatrixError .ORG_MATRIX_EXPIRED_ACCOUNT )
40+
41+ fun Throwable.isLimitExceededError () = this is Failure .ServerError &&
42+ httpCode == 429 &&
43+ error.code == MatrixError .M_LIMIT_EXCEEDED
44+
45+ fun Throwable.shouldBeRetried () = this is Failure .NetworkConnection ||
46+ this is IOException ||
47+ isLimitExceededError()
5348
5449/* *
5550 * Get the retry delay in case of rate limit exceeded error, adding 100 ms, of defaultValue otherwise
@@ -63,41 +58,33 @@ fun Throwable.getRetryDelay(defaultValue: Long): Long {
6358 ? : defaultValue
6459}
6560
66- fun Throwable.isUsernameInUse (): Boolean {
67- return this is Failure .ServerError && error.code == MatrixError .M_USER_IN_USE
68- }
61+ fun Throwable.isUsernameInUse () = this is Failure .ServerError &&
62+ error.code == MatrixError .M_USER_IN_USE
6963
70- fun Throwable.isInvalidUsername (): Boolean {
71- return this is Failure .ServerError &&
72- error.code == MatrixError .M_INVALID_USERNAME
73- }
64+ fun Throwable.isInvalidUsername () = this is Failure .ServerError &&
65+ error.code == MatrixError .M_INVALID_USERNAME
7466
75- fun Throwable.isInvalidPassword (): Boolean {
76- return this is Failure .ServerError &&
77- error.code == MatrixError .M_FORBIDDEN &&
78- error.message == " Invalid password"
79- }
67+ fun Throwable.isInvalidPassword () = this is Failure .ServerError &&
68+ error.code == MatrixError .M_FORBIDDEN &&
69+ error.message == " Invalid password"
8070
81- fun Throwable.isRegistrationDisabled (): Boolean {
82- return this is Failure .ServerError && error.code == MatrixError .M_FORBIDDEN &&
83- httpCode == HttpsURLConnection .HTTP_FORBIDDEN
84- }
71+ fun Throwable.isRegistrationDisabled () = this is Failure .ServerError &&
72+ error.code == MatrixError .M_FORBIDDEN &&
73+ httpCode == HttpsURLConnection .HTTP_FORBIDDEN
8574
86- fun Throwable.isWeakPassword (): Boolean {
87- return this is Failure .ServerError && error.code == MatrixError .M_WEAK_PASSWORD
88- }
75+ fun Throwable.isWeakPassword () = this is Failure .ServerError &&
76+ error.code == MatrixError .M_WEAK_PASSWORD
8977
90- fun Throwable.isLoginEmailUnknown (): Boolean {
91- return this is Failure .ServerError &&
92- error.code == MatrixError .M_FORBIDDEN &&
93- error.message.isEmpty()
94- }
78+ fun Throwable.isLoginEmailUnknown () = this is Failure .ServerError &&
79+ error.code == MatrixError .M_FORBIDDEN &&
80+ error.message.isEmpty()
9581
96- fun Throwable.isInvalidUIAAuth (): Boolean {
97- return this is Failure .ServerError &&
98- error.code == MatrixError .M_FORBIDDEN &&
99- error.flows != null
100- }
82+ fun Throwable.isInvalidUIAAuth () = this is Failure .ServerError &&
83+ error.code == MatrixError .M_FORBIDDEN &&
84+ error.flows != null
85+
86+ fun Throwable.isHomeserverUnavailable () = this is Failure .NetworkConnection &&
87+ this .ioException is UnknownHostException
10188
10289/* *
10390 * Try to convert to a RegistrationFlowResponse. Return null in the cases it's not possible
@@ -129,13 +116,11 @@ fun Throwable.toRegistrationFlowResponse(): RegistrationFlowResponse? {
129116 }
130117}
131118
132- fun Throwable.isRegistrationAvailabilityError (): Boolean {
133- return this is Failure .ServerError &&
134- httpCode == HttpsURLConnection .HTTP_BAD_REQUEST && /* 400 */
135- (error.code == MatrixError .M_USER_IN_USE ||
136- error.code == MatrixError .M_INVALID_USERNAME ||
137- error.code == MatrixError .M_EXCLUSIVE )
138- }
119+ fun Throwable.isRegistrationAvailabilityError () = this is Failure .ServerError &&
120+ httpCode == HttpsURLConnection .HTTP_BAD_REQUEST && /* 400 */
121+ (error.code == MatrixError .M_USER_IN_USE ||
122+ error.code == MatrixError .M_INVALID_USERNAME ||
123+ error.code == MatrixError .M_EXCLUSIVE )
139124
140125/* *
141126 * Try to convert to a ScanFailure. Return null in the cases it's not possible
0 commit comments