Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Cannot type test: interface "PouchDB.Core.PouchDB.Core.Error" for results of bulkDocs #5

Open
vschep opened this issue Dec 5, 2018 · 3 comments

Comments

@vschep
Copy link

vschep commented Dec 5, 2018

When I try to evaluate the results of bulkDocs I get a Fable compiler error. I want to check the results of bulkDocs.

Sample code (I enhanced the existing test code in CoreTests.fs with the last 5 lines of code):

let data =
  [0..100]
    |> List.map(Test.Dummy >> Test.Encode)
    |> List.toArray

let options: PouchDB.Core.BulkDocsOptions = jsOptions(fun opt ->
  opt.docs <- !!data
)
let! results = db.bulkDocs options

results
|> Seq.iter (fun result ->
    match result with
    | U2.Case1 response -> printfn "--- result: %A" result
    | U2.Case2 error -> printfn "--- error: %A" error)

You can get the full code at my fork of this repo .

Compiler error:

error FABLE: Cannot type test: interface "PouchDB.Core.PouchDB.Core.Error"

Am I doing wrong or is this perhaps a bug related to (dev2.0) Cannot type test any exception .
I attempted to solve the problem according to .

@whitetigle
Copy link
Owner

whitetigle commented Dec 6, 2018

I think you're right, I have had problems with interfaces too. Another issue mentionned there.

Basically the results is a list like this with our two types.

[{"ok":true,"id":"same Id","rev":"1-2ce5d6c2ff944f8ff3fedee7f9909190"},{"status":409,"name":"conflict","message":"Document update conflict","error":true,"id":"same Id"}]

But I think that using Thoth.Json we should be able to decode the mixed results and maybe use a real record.

I'm thinking about Thoth.Json.Decode.oneOf . I had a slightly different problem to tackle in January with Thoth v1, but I think the logic is the same.

So I will try to prepare something and incorporate this in the lib to make things easier for everybody. Meanhwile if you come up with something don't hesitate to share your solution or make a PR.

@vschep
Copy link
Author

vschep commented Dec 7, 2018

Thanks for your answer! I am not that deep into Fable to understand the problem completely but I will have one more look at it.

@whitetigle
Copy link
Owner

whitetigle commented Dec 19, 2018

Was working on some nodemailer project this morning and I had the same problem so I went with what @MangelMaxime proposed in the mentionned issue: check if field exists using magic ? operator.

So the logic is: when we get our response, since it can be a Core.Error or a Core.Response we just check if the field reason exists in the response object. If so we can then fairly assume it's a Core.Error.

So here you go:

      let data =
        [0..10]
          |> List.map(Test.Dummy >> Test.Encode)
          |> List.toArray

      let options: PouchDB.Core.BulkDocsOptions = jsOptions(fun opt ->
        opt.docs <- !!data
      )
      let! results = db.bulkDocs options
      results 
        |> Seq.iter( fun result -> 

          if isNull result?reason then // it's not an error
            let response : PouchDB.Core.Response = unbox result
            printfn "Response: %A" response.id
          else
            let error : PouchDB.Core.Error = unbox result
            printfn "Error: %A" error.reason
        )

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants