Backend InstantSearch error handling

Hi,

I’m using the Backend InstantSearch approach to build a middleware that will handle users authorisation (if and how many results they’re entitled to see on the frontend).

How do I enrich the response coming from Algolia with a custom message/error, e.g.: an user without entitlements should see a paywall rather than the results?

Thanks!

Hi @carminecolicino,

Thanks for contacting Algolia!

From your question: “How do I enrich the response coming from Algolia with a custom message/error, e.g.: an user without entitlements should see a paywall rather than the results?”

I understood this to mean that in Algolia you have an index of data (your results), some users have roles/permissions to see these results, and others do not.

I would consider Algolia as the home of your results data, however, these paywalls or error-messaging can be handled on the frontend. That is:

  • search Algolia
  • before/after your middleware determines authorisation level of user
  • if user has sufficient authorisation, show hits/filters and other InstantSearch widgets on frontend
  • if user has insufficient authorisation, hide hits/filters and other InstantSearch widgets on frontend; display your message

My own instinct would be to handle this custom error messaging client side, using CSS to hide the elements conditionally based on authorisation. I hope this helps!

Hi @ajay.david,

The results are getting truncated/removed in the middleware, they don’t have to reach the client if the user is not authenticated/authorised, so that’s not something I can do with CSS.

I just need to know where in the response object - returned from the custom Algolia client (similar to the one explained here) - I can inject additional information that can be interpreted by the Algolia widgets on the frontend.

Thanks.