box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5

Home Page:https://developer.box.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Authetication exception without internet

arangaramu opened this issue · comments

My box appl is crashing without internet.
Here is my simple code
var boxConfig = new BoxConfigBuilder(box_Client_Id, box_Client_Secret)
.SetEnterpriseId(box_Enterprise_ID).Build();
var boxCCG = new BoxCCGAuth(boxConfig);
userToken = await boxCCG.UserTokenAsync(box_User_Id);

The UserTokenAsync crashes the application without internet. I tried try and catch logic to avaid crashing the app but no luck

I get this error message. Any idea how else to fix this issue

Unhandled Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote name could not be resolved: 'api.box.com'
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Box.V2.Request.HttpRequestHandler.d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Box.V2.Request.HttpRequestHandler.d__61.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Box.V2.Services.BoxService.<ToResponseAsync>d__51.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Box.V2.CCGAuth.BoxCCGAuth.d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Box.V2.CCGAuth.BoxCCGAuth.d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Hi @arangaramu
The UserTokenAsync (like most SDK methods) makes an HTTP request to api.box.com so without the Internet, this call is expected to fail. I am not sure what you are trying to achieve, and what is the behavior you expect without the internet connection?

I want to handle the exception. I dont want to crash my application. Is there anyway I can detect the network failure or unable to establish the connection with server for any reason

The simpliest solution to not crash whole app would be to wrap call for token in try/catch clause like this

try
{
    var userToken = await boxCCG.UserTokenAsync(box_User_Id);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

You can alternatively ping some other service (like api.box.com for example) to check if you have internet connection before using boxClient.

Thank you. In the earlier code I had catch (BoxException ex) and it didnt catch the exception. I dont know the difference between two. But now issue is resolved and its working fine. Thank you agian