Try our conversational search powered by Generative AI!

Determine if Find index is connected or 401 in code

Vote:
 

My developer index timed out and this presented a great oppertunity for me to pop out friendly errors instead of a 401 in case we run into a network glitch in production

I'm trying to find a way to determine of the index invalid or unreachable  (we are using on-prem and there is a chance we can't access the index) so that I can present a friendly error instead of a system 401 error.   

The example code has a function to determine of the search in configured

private bool SearchIndexIsConfigured(EPiServer.Find.Configuration configuration)
        {
            return (!configuration.ServiceUrl.IsNullOrEmpty()
                    && !configuration.ServiceUrl.Contains("YOUR_URI")
                    && !configuration.DefaultIndex.IsNullOrEmpty()
                    && !configuration.DefaultIndex.Equals("YOUR_INDEX"));
        }

This works great, but it doesn't account for if the service is running or a 401.  Is there anything I can do to check for this?  The only thing I can see is the settings is the query settings times out.  For now I can play with that, but I'm certain there is a more graceful way to handle this.

Thanks!

#203691
May 02, 2019 19:10
Vote:
 

Hi, basically you need to handle exceptions when you are using Find and have different "friendly" results based on what the exception was.

Jonas has listed the Episerver Find exceptions in his blog post.

From end users point of view (user using your website), the fiendly error in all cases should be something like "Ooops, our search is not working." but from developers oint of view, the correct kind of log message about the reason why it failed is important.

BTW, HTTP status 401 is not time out but unauthorized.

#203740
May 05, 2019 11:11
Vote:
 

Thank you Antti, 

The biggest problem I'm running into is that my queries are still reaching out to Find despite the fact that the query is unauthorized.  Therefor I am getting timeouts.

I'm trying to find a way to determine if the service is down or 401 before the query starts running and times out.  That way I can throw a freindly message to the users within seconds intead of the minute or two it is taking me now. 

Any idea on how I can run a quick test for this in the code before the query?

#203752
May 06, 2019 16:59
Vote:
 

Hi, well I would drop the idea to first "ping" is the service alive and then take actions based on that.

I would suggest to use the 'defaultRequestTimeout' attribute on the episerver.find section in your web.config, in that attribute you can configure the default request timeout in milliseconds (the underlying HttpWebrequest uses default value of 100000ms (100 seconds) for the timeout).

<episerver.find serviceUrl="your-servuice-url-here" defaultIndex="your-index-name-here" defaultRequestTimeout="5000" />

The above setting sets the timeout to 5 seconds.

#203916
May 10, 2019 19:37
Vote:
 

That seems to be a way to do it.    Then I add a friendly message in the catch statement.  

Thank you

#204070
May 17, 2019 16:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.