Skip to content

Commit

Permalink
Handle exception when user is offline
Browse files Browse the repository at this point in the history
Summary:
There was an unhandled exception when a user tried to activate voice commands while offline. This catches that exception and surfaces it to VoiceService.events.OnError.

Addresses github issue #69

Reviewed By: ryanbailey-fb

Differential Revision: D34537991

fbshipit-source-id: edd32ff5af3a8ddd20a51f2f324acf0260a17396
  • Loading branch information
yolanother authored and facebook-github-bot committed Mar 1, 2022
1 parent f7780f0 commit c5304e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
53 changes: 34 additions & 19 deletions Scripts/Runtime/WitRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class WitRequest
public const int URI_DEFAULT_PORT = 0;

public const string WIT_API_VERSION = "20210928";
public const string WIT_SDK_VERSION = "0.0.31";
public const string WIT_SDK_VERSION = "0.0.32";

public const string WIT_ENDPOINT_SPEECH = "speech";
public const string WIT_ENDPOINT_MESSAGE = "message";
Expand Down Expand Up @@ -624,30 +624,42 @@ private void HandleResponse(IAsyncResult ar)

private void HandleRequestStream(IAsyncResult ar)
{
StartResponse();
var stream = _request.EndGetRequestStream(ar);
bytesWritten = 0;

if (null != postData)
{
bytesWritten += postData.Length;
stream.Write(postData, 0, postData.Length);
CloseRequestStream();
}
else
try
{
if (null == onInputStreamReady)
StartResponse();
var stream = _request.EndGetRequestStream(ar);
bytesWritten = 0;

if (null != postData)
{
bytesWritten += postData.Length;
stream.Write(postData, 0, postData.Length);
CloseRequestStream();
}
else
{
isRequestStreamActive = true;
SafeInvoke(onInputStreamReady);
if (null == onInputStreamReady)
{
CloseRequestStream();
}
else
{
isRequestStreamActive = true;
SafeInvoke(onInputStreamReady);
}
}

new Thread(ExecuteWriteThread).Start(stream);
}
catch (WebException e)
{
if (e.Status != WebExceptionStatus.RequestCanceled)
{
statusCode = (int) e.Status;
statusDescription = e.Message;
SafeInvoke(onResponse);
}
}

new Thread(ExecuteWriteThread).Start(stream);
}

private void ExecuteWriteThread(object obj)
Expand Down Expand Up @@ -728,8 +740,11 @@ public void AbortRequest()
{
CloseActiveStream();
_request.Abort();
statusCode = ERROR_CODE_ABORTED;
statusDescription = "Request was aborted";
if (statusCode == 0)
{
statusCode = ERROR_CODE_ABORTED;
statusDescription = "Request was aborted";
}
isActive = false;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.facebook.witai",
"displayName": "Wit.ai",
"version": "0.0.31",
"version": "0.0.32",
"samples": [
{
"displayName": "Response Debugger",
Expand Down

0 comments on commit c5304e1

Please sign in to comment.