mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-05-01 05:44:24 +02:00
refactor: migrate to custom exceptions for patch options
This commit is contained in:
parent
19256b5437
commit
1b42f65d95
@ -1,13 +1,16 @@
|
|||||||
|
@file:Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
package app.revanced.patcher.patch
|
package app.revanced.patcher.patch
|
||||||
|
|
||||||
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
|
|
||||||
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
||||||
|
class IllegalValueException(val value: Any?) : Exception("Illegal value: $value")
|
||||||
|
class InvalidTypeException(val got: String, val expected: String) :
|
||||||
|
Exception("Invalid option value type: $got, expected $expected")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A registry for an array of [PatchOption]s.
|
* A registry for an array of [PatchOption]s.
|
||||||
* @param options An array of [PatchOption]s.
|
* @param options An array of [PatchOption]s.
|
||||||
*/
|
*/
|
||||||
@Suppress("MemberVisibilityCanBePrivate")
|
|
||||||
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
||||||
private val register = buildMap {
|
private val register = buildMap {
|
||||||
for (option in options) {
|
for (option in options) {
|
||||||
@ -31,9 +34,10 @@ class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>
|
|||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
inline operator fun <reified T> set(key: String, value: T) {
|
inline operator fun <reified T> set(key: String, value: T) {
|
||||||
@Suppress("UNCHECKED_CAST") val opt = get(key) as? PatchOption<T>
|
@Suppress("UNCHECKED_CAST") val opt = get(key) as PatchOption<T>
|
||||||
if (opt == null || opt.value !is T) throw IllegalArgumentException(
|
if (opt.value !is T) throw InvalidTypeException(
|
||||||
"The type of the option value is not the same as the type value provided"
|
T::class.java.canonicalName,
|
||||||
|
opt.value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
)
|
)
|
||||||
opt.value = value
|
opt.value = value
|
||||||
}
|
}
|
||||||
@ -61,7 +65,7 @@ sealed class PatchOption<T>(
|
|||||||
var value: T? = default
|
var value: T? = default
|
||||||
set(value) {
|
set(value) {
|
||||||
if (!validator(value)) {
|
if (!validator(value)) {
|
||||||
throw IllegalArgumentException("Illegal value: $value")
|
throw IllegalValueException(value)
|
||||||
}
|
}
|
||||||
field = value
|
field = value
|
||||||
}
|
}
|
||||||
|
@ -45,14 +45,14 @@ internal class PatchOptionsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of invalid value type`() {
|
fun `should fail because of invalid value type`() {
|
||||||
assertThrows<IllegalArgumentException> {
|
assertThrows<InvalidTypeException> {
|
||||||
options["key1"] = 123
|
options["key1"] = 123
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of an illegal value`() {
|
fun `should fail because of an illegal value`() {
|
||||||
assertThrows<IllegalArgumentException> {
|
assertThrows<IllegalValueException> {
|
||||||
options["key3"] = "this value is not an allowed option"
|
options["key3"] = "this value is not an allowed option"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user