Zaid-Ajaj / Fable.Remoting

Type-safe communication layer (RPC-style) for F# featuring Fable and .NET Apps
https://zaid-ajaj.github.io/Fable.Remoting/
MIT License
272 stars 54 forks source link

Add support for optionally adding an error interceptor. Addresses #301 #303

Closed enriquein closed 2 years ago

enriquein commented 2 years ago

This adds support for optionally specifying an error interceptor for responses. The idea is that this interceptor would allow you to run centralized handlers for certain types of errors, without affecting the outcome of the response. For example, you could write an error interceptor to "handle" 401 responses by redirecting back to login, or a general 400-500 error interceptor that shows a toast notification.

The signature of the interceptor is statusCode: int -> responseBody: string -> unit. You can set up the interceptor by calling Remoting.withErrorInterceptor:

let errorInterceptor statusCode responseBody = 
    match statusCode with
    | 401 -> redirectToLogin()
    | 500 -> console.log "error"
    | _ -> () 

let musicStore = 
    Remoting.createApi()
    |> Remoting.withErrorInterceptor errorInterceptor  
    |> Remoting.buildproxy<IMusicStore>
Zaid-Ajaj commented 2 years ago

Hi there @enriquein, as mentioned in my comment, the current API already allows for writing global interceptors when needed.

enriquein commented 2 years ago

Closing this pull request since it's redundant. Thank you for the feedback!