question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling response codes

See original GitHub issue

Hi everybody, this is a question not an actual issue but I don’t know where to ask so here it goes.

I’m building an app that has a public and a private section and we are using sagas to manage side effects, most of the time we call a function that retrieves data from a backend.

Because sessions may be expired (we use jwt that provide a TTL for each token) or unauthorized we wrapped the call method to something with a little more semantic to us, here is an example:

//sagas/utils.js
import { sessionExpired, resourceForbidden, resourceUnauthorized } from '../actionCreators'

export function* callCheckingAuthentication(fn, ...rest) {
  const response = yield call(fn, ...rest)
  const code = response.code

  if(code === 419) { //419: When the session has expired
    yield put(sessionExpired())
  }
  if(code === 403) { //403: When the resource is forbidden
    yield put(resourceForbidden())
  }
  if(code === 401) { //401: When the resource is unauthorized
    yield put(resourceUnauthorized())
  }

  return Promise.resolve(response)
}

I know callCheckingAuthentication is a terrible name but it is a WIP and I can’t figure out a better name.

The main idea is to use this new function when making an authenticated request, like this:

import { dataLoaded } from '../actionCreators'
export function* dataRequested() {
  let action = null
  while(action = yield take('DATA_REQUESTED')) {
    const result = yield callCheckingAuthentication(fetchData)
    yield put(dataLoaded(result.data))
  }
}

This allow us to put relevant actions (SESSION_EXPIRED, RESOURCE_FORBIDDEN, etc) so they can be takeen by other sagas or reduced by any interested reducers. The key thing is to always return the original result so we don’t break the regular flow of the generator waiting for this result.

I was wondering if this is a valid approach or if it has any drawbacks, so far we haven’t experienced any issue with this approach but maybe somebody can came up with one.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:12
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
yelouaficommented, Feb 18, 2016

What about making the error codes handling a normal function, a function which returns an action ?

function responseAction(response) {
  const code = response.code

  if(code === 419) { //419: When the session has expired
    return sessionExpired() 
  }
  ...
  ...
  return dataLoaded(reponse.data)
}

export function* dataRequested() {
  let action = null
  while(action = yield take('DATA_REQUESTED')) {
    const result = yield call(fetchData)
    yield put(responseAction(result))
  }
}
0reactions
kfrajtakcommented, Mar 17, 2017

@cherta, that’s an interesting piece of code. I tried to implement something similar but failed with the fetchData promise. Can you share the code of that? Thanks, Karel

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
Best Practices for REST API Error Handling - Baeldung
2. HTTP Status Codes · 100-level (Informational) – server acknowledges a request · 200-level (Success) – server completed the request as expected ...
Read more >
How to handle HTTP status codes? - ios - Stack Overflow
There's actually no need to check for each and every status codes. What I would normally do is to check if the request...
Read more >
How to Handle HTTP Status Codes When Consuming a REST ...
How to Handle HTTP Status Codes When Consuming a REST API in Low-Code · Approach #1: Default Behavior · Approach #2: Handling the...
Read more >
HTTP Status Codes - REST API Tutorial
HTTP defines these standard status codes divided into five categories that can be used to convey the results of a client's request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found