Skip to content

Commit

Permalink
Added events for beamruntime for better error handling
Browse files Browse the repository at this point in the history
- Added events for error handling when the SDK initialization fails
- Added events for error handling when subsystems user authentication fails
  • Loading branch information
AlyAmeenRizk committed Oct 28, 2024
1 parent 8ad398b commit 73f1659
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,24 @@ void UBeamRuntime::TriggerOnBeamableStarting(FBeamWaitCompleteEvent Evt,
TArray<FString> Errors;
if (RequestTrackerSystem->IsWaitFailed(Evt, Errors))
{
CurrentSdkState = ESDKState::InitializationFailed;

FString Err;
for (const auto& Error : Errors) Err += Error + TEXT("\n");
UE_LOG(LogBeamRuntime, Error, TEXT("%s"), *Err);

CurrentSdkState = ESDKState::InitializationFailed;
CachedInitializationErrors.Empty();
for (auto& Op : Evt.Operations)
{
RequestTrackerSystem->TryGetOperationEvents(Op,EBeamOperationEventType::OET_ERROR,NAME_All,CachedInitializationErrors);
}
OnSDKInitializationFailed.Broadcast(CachedInitializationErrors);
OnSDKInitializationFailedCode.Broadcast(CachedInitializationErrors);

// Early out and don't initialize if errors happen here.
return;
}

// If everything is fine... so let's continue with initializing Beamable.
if (const bool bIsDedicatedServer = GetGameInstance()->IsDedicatedServerInstance())
{
Expand Down Expand Up @@ -380,12 +388,19 @@ void UBeamRuntime::TriggerOnContentReady(FBeamWaitCompleteEvent Evt,
TArray<FString> Errors;
if (RequestTrackerSystem->IsWaitFailed(Evt, Errors))
{
CurrentSdkState = ESDKState::InitializationFailed;

FString Err;
for (const auto& Error : Errors) Err += Error + TEXT("\n");
UE_LOG(LogBeamRuntime, Error, TEXT("%s"), *Err);

CurrentSdkState = ESDKState::InitializationFailed;

CachedInitializationErrors.Empty();
for (auto& Op : Evt.Operations)
{
RequestTrackerSystem->TryGetOperationEvents(Op,EBeamOperationEventType::OET_ERROR,NAME_All,CachedInitializationErrors);
}
OnSDKInitializationFailed.Broadcast(CachedInitializationErrors);
OnSDKInitializationFailedCode.Broadcast(CachedInitializationErrors);
// Early out and don't initialize if errors happen here.
return;
}
Expand Down Expand Up @@ -417,11 +432,19 @@ void UBeamRuntime::TriggerOnStartedAndFrictionlessAuth(FBeamWaitCompleteEvent Ev
TArray<FString> Errors;
if (RequestTrackerSystem->IsWaitFailed(Evt, Errors))
{
CurrentSdkState = ESDKState::InitializationFailed;

FString Err;
for (const auto& Error : Errors) Err += Error + TEXT("\n");
UE_LOG(LogBeamRuntime, Error, TEXT("%s"), *Err);

CurrentSdkState = ESDKState::InitializationFailed;
CachedInitializationErrors.Empty();
for (auto& Op : Evt.Operations)
{
RequestTrackerSystem->TryGetOperationEvents(Op,EBeamOperationEventType::OET_ERROR,NAME_All,CachedInitializationErrors);
}
OnSDKInitializationFailed.Broadcast(CachedInitializationErrors);
OnSDKInitializationFailedCode.Broadcast(CachedInitializationErrors);
// Early out and don't initialize if errors happen here.
return;
}
Expand Down Expand Up @@ -520,6 +543,14 @@ void UBeamRuntime::TriggerSubsystemPostUserSignIn(FBeamWaitCompleteEvent Evt, FU
for (const auto& Error : Errors) Err += Error + TEXT("\n");
UE_LOG(LogBeamRuntime, Error, TEXT("%s"), *Err);

TArray<FBeamOperationEvent> ErrorEvents;
for (auto& Op : Evt.Operations)
{
RequestTrackerSystem->TryGetOperationEvents(Op,EBeamOperationEventType::OET_ERROR,NAME_All,ErrorEvents);
}

OnSubsystemsUserInitializationFailed.Broadcast(UserSlot,ErrorEvents);
OnSubsystemsUserInitializationFailedCode.Broadcast(UserSlot,ErrorEvents);
// Early out and don't initialize if errors happen here.
return;
}
Expand Down Expand Up @@ -556,6 +587,15 @@ void UBeamRuntime::TriggerSubsystemPostUserSignIn(FBeamWaitCompleteEvent Evt, FU
for (const auto& Error : Errors) Err += Error + TEXT("\n");
UE_LOG(LogBeamRuntime, Error, TEXT("%s"), *Err);

TArray<FBeamOperationEvent> ErrorEvents;
for (auto& Op : PostEvt.Operations)
{
RequestTrackerSystem->TryGetOperationEvents(Op,EBeamOperationEventType::OET_ERROR,NAME_All,ErrorEvents);
}

OnSubsystemsUserInitializationFailed.Broadcast(UserSlot,ErrorEvents);
OnSubsystemsUserInitializationFailedCode.Broadcast(UserSlot,ErrorEvents);

// Early out and don't initialize if errors happen here.
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,17 @@ DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FUserStateChangedEvent, const FUserS

DECLARE_MULTICAST_DELEGATE_OneParam(FUserStateChangedEventCode, FUserSlot);

DECLARE_DYNAMIC_DELEGATE_OneParam(FRuntimeError,const TArray<FBeamOperationEvent>&, OperationEventsWithErrors);

DECLARE_DELEGATE_OneParam(FRuntimeErrorCode,const TArray<FBeamOperationEvent>&);

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSDKInitilizationErrorEvent,const TArray<FBeamOperationEvent>&, OperationEventsWithErrors);

DECLARE_MULTICAST_DELEGATE_OneParam(FSDKInitilizationErrorEventCode,const TArray<FBeamOperationEvent>&) ;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSubsystemsUserInitializationErrorEvent,const FUserSlot&,UserSlot,const TArray<FBeamOperationEvent>&, OperationEventsWithErrors);

DECLARE_MULTICAST_DELEGATE_TwoParams(FSubsystemsUserInitializationErrorEventCode, const FUserSlot&,const TArray<FBeamOperationEvent>&);
/**
* State of SDK intialization.
*
Expand Down Expand Up @@ -323,6 +333,18 @@ class BEAMABLECORERUNTIME_API UBeamRuntime : public UGameInstanceSubsystem
FRuntimeStateChangedEvent OnStarted;
FRuntimeStateChangedEventCode OnStartedCode;

/**
* @brief This is called when the operations for starting the sdk fails.
*/
UPROPERTY()
FSDKInitilizationErrorEvent OnSDKInitializationFailed;
FSDKInitilizationErrorEventCode OnSDKInitializationFailedCode;
/**
* @brief This is a list of the operation that ended with errors after the initialization fails.
*/
UPROPERTY()
TArray<FBeamOperationEvent> CachedInitializationErrors;

/**
* @brief Every time a user signs into beamable, we give each subsystem the ability to run an operation for that user.
* We also give them the list of currently authenticated UserSlots (so that they can tell if the user that just signed in is the last one for example).
Expand Down Expand Up @@ -355,7 +377,6 @@ class BEAMABLECORERUNTIME_API UBeamRuntime : public UGameInstanceSubsystem
*/
UPROPERTY()
FRuntimeStateChangedEvent OnReady;

FRuntimeStateChangedEventCode OnReadyCode;

/**
Expand Down Expand Up @@ -519,6 +540,13 @@ class BEAMABLECORERUNTIME_API UBeamRuntime : public UGameInstanceSubsystem
FUserStateChangedEvent OnUserCleared;
FUserStateChangedEventCode OnUserClearedCode;

/**
* @brief This is called when the initialization of the subsystems user slots fails.
*/
UPROPERTY()
FSubsystemsUserInitializationErrorEvent OnSubsystemsUserInitializationFailed;
FSubsystemsUserInitializationErrorEventCode OnSubsystemsUserInitializationFailedCode;

/**
* @brief In BP, use this function to bind initialization functions to OnReady. This will execute the delegate if you're already ready before it binds it.
*/
Expand Down Expand Up @@ -558,6 +586,45 @@ class BEAMABLECORERUNTIME_API UBeamRuntime : public UGameInstanceSubsystem
OnReadyCode.Remove(Handle);
}

/**
* @brief In BP, use this function to bind error handling logic to OnSDKInitializationFailed. This will execute the delegate if sdk already failed initialization before it binds it.
*/
UFUNCTION(BlueprintCallable)
void RegisterOnSDKInitializationFailed(FRuntimeError Handler)
{
if (CurrentSdkState == ESDKState::InitializationFailed) const auto _ = Handler.ExecuteIfBound(CachedInitializationErrors);
OnSDKInitializationFailed.Add(Handler);
}

/**
* @brief In BP, use this function to bind error handling logic to OnSDKInitializationFailed. This will NOT execute the delegate if initialization already failed.
*/
UFUNCTION(BlueprintCallable)
void RegisterOnSDKInitializationFailed_NoExecute(FRuntimeError Handler) { OnSDKInitializationFailed.Add(Handler); }

/**
* @brief In BP, use this function to unbind error handling events to OnSDKInitializationFailed.
*/
UFUNCTION(BlueprintCallable)
void UnRegisterOnSDKInitializationFailed(FRuntimeError Handler)
{
if (OnSDKInitializationFailed.Contains(Handler))
OnSDKInitializationFailed.Remove(Handler);
}

FDelegateHandle CPP_RegisterOnSDKInitializationFailed(FRuntimeErrorCode Handler)
{
if (bDidFirstAuthRun) const auto _ = Handler.ExecuteIfBound(CachedInitializationErrors);
return OnSDKInitializationFailedCode.Add(Handler);
}

FDelegateHandle CPP_RegisterOnSDKInitializationFailed_NoExecute(FRuntimeErrorCode Handler) { return OnSDKInitializationFailedCode.Add(Handler); }

void CPP_UnRegisterOnSDKInitializationFailed(FDelegateHandle Handle)
{
OnSDKInitializationFailedCode.Remove(Handle);
}

/**
* @brief An operation that will authenticate a user with the beamable and persist that authentication locally.
*/
Expand Down

0 comments on commit 73f1659

Please sign in to comment.