meafmira / bs-axios

Bucklescript bindings for axios
71 stars 23 forks source link

Intercept 401 responses #15

Closed lukashambsch closed 6 years ago

lukashambsch commented 6 years ago

Is there a way to intercept 401 responses like with axios?

Here's an example: https://gist.github.com/yajra/5f5551649b20c8f668aec48549ef5c1f

lukashambsch commented 6 years ago

@meafmira Any ideas on how to do something like this? Or, is there a way I can at least access the status code of a 401 response? That way I would be able to handle the redirection there.

meafmira commented 6 years ago

@lukashambsch I think you should use something like this:

external promiseErrorToJsObj : Js.Promise.error => Js.t('a) = "%identity";

Js.Promise.(
  Instance.get(inst, "/")
  |> then_(resp => resolve(Belt.Result.Ok(resp)))
  |> catch(error => {
       let error = error |> promiseErrorToJsObj;
       Js.log(error##response##status);
       resolve(Belt.Result.Error(error));
     })
);

We need to use external conversion function because of special behaviour of standard Js.Promise module that returns Js.Promise.error type.

I think we should add something error handler for this library. I'll do this soon. Thank you for this issue

meafmira commented 6 years ago

I'll also add this example to readme file

meafmira commented 6 years ago

@lukashambsch added readme here https://github.com/meafmira/bs-axios#error-handling

lukashambsch commented 6 years ago

@meafmira Thanks! I really appreciate the response and love the library!