Using the library when the JSON data can be successful or an error message #61
Unanswered
ManjitBedi
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi ManjitBedi, please excuse the delay. From what I see you are using AnyPublisher<Any, Error> which is the "json" one already. In the failure block, in order to access the error JSON, you need to parse the Here is an example using the async await api of parsing the error JSON Payload and bridging it to a Domain Error. public func validate(user: User, with token: String) async throws {
do {
return try await put("/users/\(user.id)/validate", body: .urlEncoded(["token": token]))
} catch {
if let e = error as? NetworkingError, let payload = e.jsonPayload, let jsonData = try? JSONSerialization.data(withJSONObject: payload, options: []) {
let decoder = JSODecoder()
let validationError = try? decoder.decode(ValidationErrorResponseJSON.self, from: jsonData)
if validationError?.error == "EmailAlreadyValidated" {
throw MyDomainError.emailAlreadyValidated
}
}
throw MyDomainError.unknown
}
} Hope it helps :) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am currently writing some code where I am using the Networking library to fetch code from a server using an access token and other parameters. If there is a problem with the body data in the request, an error response will be returned as JSON.
How do I could something using Networking & Combine to handle the data that comes back that can be one of two different responses per se. I am using something like this at the moment:
This code is not going to work - well it requires a lot of boilderplate; the data in the closure is a Dictionary not JSON data. I did try changing the type of
responseData
to Any but in the debugger it is indicating that it is still a dictionary.How. might I rewrite the code using a JSON publisher?
let jsonPublisher: AnyPublisher<Any, Error> = client.get("")
Beta Was this translation helpful? Give feedback.
All reactions