Skip to content

Commit c6da90f

Browse files
chore: make parsing errors more granular
1 parent 41828fb commit c6da90f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Sources/web3swift/Utils/EIP/EIP712/EIP712Parser.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,18 @@ public class EIP712Parser {
102102
public static func parse(_ rawJson: Data) throws -> EIP712TypedData {
103103
let decoder = JSONDecoder()
104104
let types = try decoder.decode(EIP712TypeArray.self, from: rawJson).types
105-
guard let json = try rawJson.asJsonDictionary(),
106-
let primaryType = json["primaryType"] as? String,
107-
let domain = json["domain"] as? [String : AnyObject],
108-
let message = json["message"] as? [String : AnyObject]
109-
else {
110-
throw Web3Error.inputError(desc: "EIP712Parser: cannot decode EIP712TypedData object. Failed to parse one of primaryType, domain or message fields. Is any field missing?")
105+
guard let json = try rawJson.asJsonDictionary() else {
106+
throw Web3Error.inputError(desc: "EIP712Parser. Cannot decode given JSON as it cannot be represented as a Dictionary. Is it valid JSON?")
107+
}
108+
guard let primaryType = json["primaryType"] as? String else {
109+
throw Web3Error.inputError(desc: "EIP712Parser. Top-level string field 'primaryType' missing.")
110+
}
111+
guard let domain = json["domain"] as? [String : AnyObject] else {
112+
throw Web3Error.inputError(desc: "EIP712Parser. Top-level object field 'domain' missing.")
113+
}
114+
guard let message = json["message"] as? [String : AnyObject] else {
115+
throw Web3Error.inputError(desc: "EIP712Parser. Top-level object field 'message' missing.")
111116
}
112-
113117
return try EIP712TypedData(types: types, primaryType: primaryType, domain: domain, message: message)
114118
}
115119
}

0 commit comments

Comments
 (0)