Skip to content

Commit

Permalink
Improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
valentunn committed Jun 3, 2024
1 parent 012b8ce commit f54fa90
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
// App version
versionName = '2.1.1'
versionName = '2.1.2'
versionCode = 1

// SDK and tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.novasama.substrate_sdk_android.runtime.RuntimeSnapshot
import io.novasama.substrate_sdk_android.runtime.definitions.types.RuntimeType
import io.novasama.substrate_sdk_android.runtime.definitions.types.bytes
import io.novasama.substrate_sdk_android.runtime.definitions.types.errors.EncodeDecodeException
import io.novasama.substrate_sdk_android.runtime.metadata.module.ErrorMetadata
import io.novasama.substrate_sdk_android.runtime.metadata.module.Event
import io.novasama.substrate_sdk_android.runtime.metadata.module.MetadataFunction
import io.novasama.substrate_sdk_android.runtime.metadata.module.Module
Expand Down Expand Up @@ -68,6 +69,11 @@ fun Module.event(name: String): Event = eventOrNull(name) ?: throw NoSuchElement

fun Module.eventOrNull(name: String): Event? = events?.get(name)

fun Module.error(index: Int): ErrorMetadata = errorOrNull(index)
?: throw NoSuchElementException("Error $index was not found in module $name")

fun Module.errorOrNull(index: Int): ErrorMetadata? = errors[index]

/**
* Unified representation of [StorageEntryType] argument types
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.novasama.substrate_sdk_android.runtime.metadata.RuntimeMetadataReader
import io.novasama.substrate_sdk_android.runtime.metadata.SignedExtensionMetadata
import io.novasama.substrate_sdk_android.runtime.metadata.groupByName
import io.novasama.substrate_sdk_android.runtime.metadata.module.Constant
import io.novasama.substrate_sdk_android.runtime.metadata.module.Error
import io.novasama.substrate_sdk_android.runtime.metadata.module.ErrorMetadata
import io.novasama.substrate_sdk_android.runtime.metadata.module.Event
import io.novasama.substrate_sdk_android.runtime.metadata.module.FunctionArgument
import io.novasama.substrate_sdk_android.runtime.metadata.module.MetadataFunction
Expand Down Expand Up @@ -194,18 +194,19 @@ object PostV14RuntimeBuilder : RuntimeBuilder {
private fun buildErrors(
typeRegistry: TypeRegistry,
errorsRaw: EncodableStruct<PalletErrorMetadataV14>,
): Map<String, Error> {
): Map<Int, ErrorMetadata> {

val type = typeRegistry[errorsRaw[PalletErrorMetadataV14.type].toString()]

if (type !is DictEnum) return emptyMap()

return type.elements.values.map {
Error(
name = it.name,
return type.elements.entries.map { (variantIndex, variantValue) ->
ErrorMetadata(
index = variantIndex,
name = variantValue.name,
documentation = emptyList(),
)
}.groupByName()
}.associateBy { it.index }
}

private fun buildEntryType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import io.novasama.substrate_sdk_android.runtime.metadata.StorageEntryMetadataSc
import io.novasama.substrate_sdk_android.runtime.metadata.StorageMetadataSchema
import io.novasama.substrate_sdk_android.runtime.metadata.groupByName
import io.novasama.substrate_sdk_android.runtime.metadata.module.Constant
import io.novasama.substrate_sdk_android.runtime.metadata.module.Error
import io.novasama.substrate_sdk_android.runtime.metadata.module.ErrorMetadata
import io.novasama.substrate_sdk_android.runtime.metadata.module.Event
import io.novasama.substrate_sdk_android.runtime.metadata.module.FunctionArgument
import io.novasama.substrate_sdk_android.runtime.metadata.module.MetadataFunction
Expand Down Expand Up @@ -162,13 +162,14 @@ internal object V13RuntimeBuilder : RuntimeBuilder {

private fun buildErrors(
errorsRaw: List<EncodableStruct<ErrorMetadataSchema>>,
): Map<String, Error> {
return errorsRaw.map { errorStruct ->
Error(
): Map<Int, ErrorMetadata> {
return errorsRaw.mapIndexed { index, errorStruct ->
ErrorMetadata(
index = index,
name = errorStruct[ErrorMetadataSchema.name],
documentation = errorStruct[ErrorMetadataSchema.documentation]
)
}.groupByName()
}.associateBy { it.index }
}

private fun buildEntryType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Module(
val calls: Map<String, MetadataFunction>?,
val events: Map<String, Event>?,
val constants: Map<String, Constant>,
val errors: Map<String, Error>,
val errors: Map<Int, ErrorMetadata>,
val index: BigInteger
) : WithName

Expand Down Expand Up @@ -72,7 +72,8 @@ class Constant(
val documentation: List<String>
) : WithName

class Error(
class ErrorMetadata(
val index: Int,
override val name: String,
val documentation: List<String>
) : WithName

0 comments on commit f54fa90

Please sign in to comment.