Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle Firebase.Database.FirebaseException raised by Firebase.Database.Streaming.FirebaseSubscription`1.<ReceiveThread>d__14.MoveNext() #87

Closed
ywlau opened this issue Jul 12, 2017 · 10 comments
Labels

Comments

@ywlau
Copy link

ywlau commented Jul 12, 2017

subscription codes:

private void init()
{
try
{
var ref= firebase.Child("Infos/updates");
var observable = ref.AsObservable();
subscription = observable
.Where(f => !string.IsNullOrEmpty(f.Key))
.Subscribe(f => processInfoUpdate(f.Object));
}
catch (Exception ex)
{
}
}

private void processInfoUpdate(InfoUpdate update)
{
try
{
//process update
}
catch (Exception ex)
{
}
}

The code was running ok initially; after sometime, the following exception is thrown.
Tried putting the relevant codes in try catch block, but doesn't help.
What's the best way to handle this exception ?

Firebase.Database.FirebaseException: Exception occured while processing the request.
Url: https://myproject.firebaseio.com/Infos/updates/.json?auth=verylongauthstring
Request Data:
Response: ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Firebase.Database.Streaming.FirebaseSubscription1.<ReceiveThread>d__14.MoveNext() --- End of inner exception stack trace --- at System.Reactive.PlatformServices.DefaultExceptionServices.Rethrow(Exception exception) at System.Reactive.Stubs.<>c.<.cctor>b__2_1(Exception ex) at System.Reactive.AnonymousSafeObserver1.OnError(Exception error)
at System.Reactive.Linq.ObservableImpl.Where1._.OnError(Exception error) at System.Reactive.AutoDetachObserver1.OnErrorCore(Exception exception)
at System.Reactive.ObserverBase1.OnError(Exception error) at Firebase.Database.Streaming.FirebaseSubscription1.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__6_1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

@bezysoftware
Copy link
Collaborator

Response status code does not indicate success: 401 (Unauthorized).

This means the auth token is not valid

@ywlau
Copy link
Author

ywlau commented Jul 12, 2017

how should i handle it when the auth token is expired (the code was running fine initially) ?

btw, AuthLink_FirebaseAuthRefreshed event is not raised.

Thanks

@bezysoftware
Copy link
Collaborator

bezysoftware commented Jul 12, 2017

Refreshing token is described here: step-up-labs/firebase-authentication-dotnet#14 (comment)

@ywlau
Copy link
Author

ywlau commented Jul 12, 2017

incorporated the refreshing token strategy and ran the app. twice; with subscription and without.

auth.FirebaseAuthRefreshed is not called in both runs and Firebase.Database.FirebaseException is thrown only if subscription is included.

I'm using FirebaseAuthentication.net v3.0.5 and FirebaseDatabase.net v3.0.0

private void init()
{
	authProvider = new FirebaseAuthProvider(new FirebaseConfig(FirebaseAppKey));
	auth = Task.Run(() => authProvider.SignInWithEmailAndPasswordAsync("myemail", "mypassword")).Result;
	auth.FirebaseAuthRefreshed += AuthLink_FirebaseAuthRefreshed;

        ......
}


private void AuthLink_FirebaseAuthRefreshed(object sender, FirebaseAuthEventArgs e)
{
	auth = Task.Run(() => auth.GetFreshAuthAsync()).Result;

	firebase = new FirebaseClient(
	FirebaseAppUri,
	new FirebaseOptions
	{
		AuthTokenAsyncFactory = () => Task.FromResult(auth.FirebaseToken)
	});
}

@ywlau
Copy link
Author

ywlau commented Jul 13, 2017

from Firebase.Auth source code; it seems we need to call the following method to trigger the auth.FirebaseAuthRefreshed event, right ?

var freshAuth = auth.GetFreshAuthAsync().Result;

if so, what's the best practice; call regularly in timer ?

Thank You.

@bezysoftware
Copy link
Collaborator

Use it in FirebaseOptions:

new FirebaseOptions
	{
		AuthTokenAsyncFactory = async () => (await auth.GetFreshAuthAsync()).FirebaseToken
	});

FirebaseAuthRefreshed event is raised to give you a chance to save the token to storage & load it upon application restart (e.g. user logs in with facebook and the token is kept so he doesn't need to login again after app restart)

@ywlau
Copy link
Author

ywlau commented Jul 15, 2017

just some feedback; in addition of refreshing token strategy, i also added err handling in subscription to handle all other potential exception gracefully.

private void init()
{
try
{
   var ref= firebase.Child("Infos/updates");
   var observable = ref.AsObservable();
   subscription = observable
      .Where(f => !string.IsNullOrEmpty(f.Key))
      .Subscribe(f => processInfoUpdate(f.Object),
         ex =>processInfoErr(),
         () => processInfoComplete();
}
catch (Exception ex)
{
}
}

thank you again for the good work

btw, where can i find the latest source code of FirebaseDatabase.net project (Nuget v3.0.5 and Github v.2.0.0) ?

@bezysoftware
Copy link
Collaborator

What do you mean where you can find sources for the nuget? They are all here on github: https://github.com/step-up-labs/firebase-database-dotnet

@stale
Copy link

stale bot commented Oct 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 2, 2019
@stale
Copy link

stale bot commented Oct 9, 2019

Closing the issue due to inactivity. Feel free to re-open

@stale stale bot closed this as completed Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants