using expression bodies for boolean checks

- moves first expression line  onto the declaration line
This commit is contained in:
Adam Brown 2022-04-14 16:10:22 +01:00
parent 11ef4a7fec
commit eda1d9142c
1 changed files with 41 additions and 62 deletions

View File

@ -25,32 +25,26 @@ import java.io.IOException
import java.net.UnknownHostException import java.net.UnknownHostException
import javax.net.ssl.HttpsURLConnection import javax.net.ssl.HttpsURLConnection
fun Throwable.is401() = fun Throwable.is401() = this is Failure.ServerError &&
this is Failure.ServerError && httpCode == HttpsURLConnection.HTTP_UNAUTHORIZED && /* 401 */
httpCode == HttpsURLConnection.HTTP_UNAUTHORIZED && /* 401 */ error.code == MatrixError.M_UNAUTHORIZED
error.code == MatrixError.M_UNAUTHORIZED
fun Throwable.is404() = fun Throwable.is404() = this is Failure.ServerError &&
this is Failure.ServerError && httpCode == HttpsURLConnection.HTTP_NOT_FOUND && /* 404 */
httpCode == HttpsURLConnection.HTTP_NOT_FOUND && /* 404 */ error.code == MatrixError.M_NOT_FOUND
error.code == MatrixError.M_NOT_FOUND
fun Throwable.isTokenError() = fun Throwable.isTokenError() = this is Failure.ServerError &&
this is Failure.ServerError && (error.code == MatrixError.M_UNKNOWN_TOKEN ||
(error.code == MatrixError.M_UNKNOWN_TOKEN || error.code == MatrixError.M_MISSING_TOKEN ||
error.code == MatrixError.M_MISSING_TOKEN || error.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT)
error.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT)
fun Throwable.isLimitExceededError() = fun Throwable.isLimitExceededError() = this is Failure.ServerError &&
this is Failure.ServerError && httpCode == 429 &&
httpCode == 429 && error.code == MatrixError.M_LIMIT_EXCEEDED
error.code == MatrixError.M_LIMIT_EXCEEDED
fun Throwable.shouldBeRetried(): Boolean { fun Throwable.shouldBeRetried() = this is Failure.NetworkConnection ||
return this is Failure.NetworkConnection || this is IOException ||
this is IOException || isLimitExceededError()
this.isLimitExceededError()
}
/** /**
* Get the retry delay in case of rate limit exceeded error, adding 100 ms, of defaultValue otherwise * Get the retry delay in case of rate limit exceeded error, adding 100 ms, of defaultValue otherwise
@ -64,46 +58,33 @@ fun Throwable.getRetryDelay(defaultValue: Long): Long {
?: defaultValue ?: defaultValue
} }
fun Throwable.isUsernameInUse(): Boolean { fun Throwable.isUsernameInUse() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_USER_IN_USE error.code == MatrixError.M_USER_IN_USE
}
fun Throwable.isInvalidUsername(): Boolean { fun Throwable.isInvalidUsername() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_INVALID_USERNAME
error.code == MatrixError.M_INVALID_USERNAME
}
fun Throwable.isInvalidPassword(): Boolean { fun Throwable.isInvalidPassword() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_FORBIDDEN &&
error.code == MatrixError.M_FORBIDDEN && error.message == "Invalid password"
error.message == "Invalid password"
}
fun Throwable.isRegistrationDisabled(): Boolean { fun Throwable.isRegistrationDisabled() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_FORBIDDEN && error.code == MatrixError.M_FORBIDDEN &&
httpCode == HttpsURLConnection.HTTP_FORBIDDEN httpCode == HttpsURLConnection.HTTP_FORBIDDEN
}
fun Throwable.isWeakPassword(): Boolean { fun Throwable.isWeakPassword() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_WEAK_PASSWORD error.code == MatrixError.M_WEAK_PASSWORD
}
fun Throwable.isLoginEmailUnknown(): Boolean { fun Throwable.isLoginEmailUnknown() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_FORBIDDEN &&
error.code == MatrixError.M_FORBIDDEN && error.message.isEmpty()
error.message.isEmpty()
}
fun Throwable.isInvalidUIAAuth(): Boolean { fun Throwable.isInvalidUIAAuth() = this is Failure.ServerError &&
return this is Failure.ServerError && error.code == MatrixError.M_FORBIDDEN &&
error.code == MatrixError.M_FORBIDDEN && error.flows != null
error.flows != null
}
fun Throwable.isHomeserverUnavailable(): Boolean { fun Throwable.isHomeserverUnavailable() = this is Failure.NetworkConnection &&
return this is Failure.NetworkConnection && this.ioException is UnknownHostException
this.ioException is UnknownHostException
}
/** /**
* Try to convert to a RegistrationFlowResponse. Return null in the cases it's not possible * Try to convert to a RegistrationFlowResponse. Return null in the cases it's not possible
@ -135,13 +116,11 @@ fun Throwable.toRegistrationFlowResponse(): RegistrationFlowResponse? {
} }
} }
fun Throwable.isRegistrationAvailabilityError(): Boolean { fun Throwable.isRegistrationAvailabilityError() = this is Failure.ServerError &&
return this is Failure.ServerError && httpCode == HttpsURLConnection.HTTP_BAD_REQUEST && /* 400 */
httpCode == HttpsURLConnection.HTTP_BAD_REQUEST && /* 400 */ (error.code == MatrixError.M_USER_IN_USE ||
(error.code == MatrixError.M_USER_IN_USE || error.code == MatrixError.M_INVALID_USERNAME ||
error.code == MatrixError.M_INVALID_USERNAME || error.code == MatrixError.M_EXCLUSIVE)
error.code == MatrixError.M_EXCLUSIVE)
}
/** /**
* Try to convert to a ScanFailure. Return null in the cases it's not possible * Try to convert to a ScanFailure. Return null in the cases it's not possible