Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why my DecodingError.dataCorrupted? I dont see any reason! #54

Closed
saroar opened this issue Sep 28, 2022 · 2 comments
Closed

Why my DecodingError.dataCorrupted? I dont see any reason! #54

saroar opened this issue Sep 28, 2022 · 2 comments

Comments

@saroar
Copy link

saroar commented Sep 28, 2022

I love this SPM but having also lots of pain after I am using this :(

my model

public struct UserGetObject: Codable {

    public var id: ObjectId
    public var fullName: String
    public var phoneNumber: String?
    public var email: String?
    public var role: UserRole
    public var language: UserLanguage

    public init(
        id: ObjectId,
        fullName: String,
        phoneNumber: String? = nil,
        email: String? = nil,
        role: UserRole,
        language: UserLanguage
    ) {
        self.id = id
        self.fullName = fullName
        self.phoneNumber = phoneNumber
        self.email = email
        self.role = role
        self.language = language
    }
}

public let usersRouter = OneOf {
    Route(.case(UsersRoute.user)) {
        Path { Parse(.string) }
        userRouter
    }

    Route(.case(UsersRoute.update)) {
        Method.put
        Body(.json(UserGetObject.self))
    }
}

// reducer
case .nameForm:
        withAnimation { state.selectedPage = .schaduleForm }

        var currentUser = environment.currentUser
        currentUser.fullName = state.name

        return .task { [cuser = currentUser] in
            return .userUpdate(
                await TaskResult {
                    try await environment.userClient.update(cuser)
                }
            )
        }


// Clinet
public struct UserClient {

    static public let apiClient: URLRoutingClient<SiteRoute> = .live(
      router: siteRouter.baseRequestData(
          .init(
              scheme: EnvironmentKeys.rootURL.scheme,
              host: EnvironmentKeys.rootURL.host,
              port: 7070,
              headers: ["Authorization": ["Bearer \(accessTokenTemp)"]]
          )
      )
    )

    public typealias UserMeHandler = @Sendable (String) async throws -> UserGetObject
    public typealias UserUpdateHandler = @Sendable (UserGetObject) async throws -> UserGetObject
    public typealias UserDeleteHandler = @Sendable (String) async throws -> Bool

    public let userMeHandler: UserMeHandler
    public let update: UserUpdateHandler
    public let delete: UserDeleteHandler

    public init(
        userMeHandler: @escaping UserMeHandler,
        update: @escaping UserUpdateHandler,
        delete: @escaping UserDeleteHandler
    ) {
        self.userMeHandler = userMeHandler
        self.update = update
        self.delete = delete
    }
}

extension UserClient {
    public static var live: UserClient =
    .init(
        userMeHandler: { id in
            return try await UserClient.apiClient.decodedResponse(
                for: .authEngine(.users(.user(id: id, route: .find))),
                as: UserGetObject.self,
                decoder: .iso8601
            ).value
        },
      update: { userInput in
          return try await UserClient.apiClient.decodedResponse(
            for: .authEngine(.users(.update(input: userInput))),
            as: UserGetObject.self,
            decoder: .iso8601
          ).value
      },
      delete: { id in

          return try await UserClient.apiClient.data(
            for: .authEngine(.users(.user(id: id, route: .delete)))
          ).response.isResponseOK()
      }
    )
}

received action:
  AppAction.welcome(
    WelcomeAction.userUpdate(
      TaskResult.failure(
        URLRoutingDecodingError(
          bytes: Data(77 bytes),
          response: NSHTTPURLResponse(),
          underlyingError: DecodingError.dataCorrupted(
            DecodingError.Context(
              codingPath: [],
              debugDescription: "The given data was not valid JSON.",
              underlyingError: NSError(
                domain: "NSCocoaErrorDomain",
                code: 3840,
                userInfo: [
                  "NSDebugDescription": "Invalid value around line 1, column 0.",
                  "NSJSONSerializationErrorIndex": 0
                ]
              )
            )
          )
        )
      )
    )
  )
  (No state changes)

https://vimeo.com/754632355

@saroar
Copy link
Author

saroar commented Sep 29, 2022

Screenshot 2022-09-29 at 11 51 19

no issue in back-end here is test from backend

DecodingError.zip

here are example project I create

@mbrandonw
Copy link
Member

Hi @saroar, I'm converting this to a discussion because it's not clear it's an issue with the library.

Also, the project you attached doesn't seem to do anything. There are no tests and there is no UI. In order for anyone to be able to help it would be best for you to distill the essence of the problem into a standalone project with no dependencies other than the swift-url-routing library.

@pointfreeco pointfreeco locked and limited conversation to collaborators Sep 29, 2022
@mbrandonw mbrandonw converted this issue into discussion #55 Sep 29, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants