Skip to content
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

How do I get the HTTP status code in the error case? #10

Open
njlr opened this issue May 13, 2020 · 4 comments
Open

How do I get the HTTP status code in the error case? #10

njlr opened this issue May 13, 2020 · 4 comments

Comments

@njlr
Copy link

njlr commented May 13, 2020

It looks like the response gets turned into a string for all response codes other than 2xx:

else errorString response |> failwith)

However, I need to handle HTTP codes differently in my app.

How can I get the HTTP code for error cases (e.g. 401)?

@MangelMaxime
Copy link
Member

Hello with the current implementation you can't access it, it needs to be rewritten.

For example, in Thoth.Fetch we use a custom implementation to preserve the error information.

Right now, I guess there is 3 possibility:

  • Someone steps in to improve Fable.Fetch
  • Write your own implementation inside of your project
  • Use a library which provides support for it like Thoth.Fetch, perhaps there are others I don't know. If you are using Thoth.Json I can only encourage you to use Thoth.Fetch :) Just for clarity, I am the maintainer of it.

@njlr
Copy link
Author

njlr commented May 18, 2020

I started using SimpleHttp, but only because that's the one I stumbled upon first.

I will leave this open since having the error code seems like an important addition to Fable.Fetch.

@forki
Copy link

forki commented May 19, 2020

@MangelMaxime we should provide a method that just runs https://github.com/fable-compiler/fable-fetch/blob/master/src/Fetch.fs#L375 without throwing if result is not ok

@MangelMaxime
Copy link
Member

@forki Indeed, it should be enough I think for fetch method but not for tryFetch.

I guess for tryFetch we will do something like that:

let tryFetch (url: string) (init: RequestProperties list) : JS.Promise<Result<Response, Response>> =
    GlobalFetch.fetch(RequestInfo.Url url, requestProps init)
    |> Promise.map (fun response ->
        if response.Ok
        then Ok response
        else Error response)

I didn't test the code so it is possible that it doesn't compile.

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

No branches or pull requests

3 participants