Skip to content

Commit 99d175a

Browse files
authored
Merge pull request #302 from watson-developer-cloud/265-error-handling
Error handling
2 parents 6e051be + 2451894 commit 99d175a

File tree

74 files changed

+7405
-6242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+7405
-6242
lines changed

Examples/ServiceExamples/Scripts/ExampleAlchemyDataNews.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using IBM.Watson.DeveloperCloud.Logging;
2222
using IBM.Watson.DeveloperCloud.Utilities;
2323
using System.Collections;
24+
using IBM.Watson.DeveloperCloud.Connection;
2425

2526
public class ExampleAlchemyDataNews : MonoBehaviour
2627
{
@@ -49,7 +50,8 @@ private IEnumerator Examples()
4950
queryFields.Add(Fields.EnrichedUrlRelationsRelationSubjectText, "Obama");
5051
queryFields.Add(Fields.EnrichedUrlCleanedtitle, "Washington");
5152
string[] returnFields = { Fields.EnrichedUrlEntities, Fields.EnrichedUrlKeywords };
52-
if (!_alchemyAPI.GetNews(OnGetNews, returnFields, queryFields))
53+
54+
if (!_alchemyAPI.GetNews(OnGetNewsSuccess, OnFail, returnFields, queryFields))
5355
Log.Debug("ExampleAlchemyDataNews.GetNews()", "Failed to get news!");
5456

5557
while (!_getNewsTested)
@@ -58,9 +60,14 @@ private IEnumerator Examples()
5860
Log.Debug("ExampleAlchemyDataNews.Examples()", "Alchemy data news examples complete!");
5961
}
6062

61-
private void OnGetNews(NewsResponse newsData, string data)
63+
private void OnGetNewsSuccess(NewsResponse resp, Dictionary<string, object> customData)
6264
{
63-
Log.Debug("ExampleAlchemyDataNews.OnGetNews()", "Alchemy data news - Get news Response: {0}", data);
6465
_getNewsTested = true;
66+
Log.Debug("ExampleAlchemyDataNews.OnSuccess()", "Response received: {0}", customData["json"].ToString());
67+
}
68+
69+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
70+
{
71+
Log.Error("ExampleAlchemyDataNews.OnFail()", "Error received: {0}", error.ToString());
6572
}
6673
}

Examples/ServiceExamples/Scripts/ExampleAlchemyLanguage.cs

Lines changed: 149 additions & 141 deletions
Large diffs are not rendered by default.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Watson.DeveloperCloud.Connection;
19+
using IBM.Watson.DeveloperCloud.Logging;
20+
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
21+
using IBM.Watson.DeveloperCloud.Services.Discovery.v1;
22+
using IBM.Watson.DeveloperCloud.Utilities;
23+
using System.Collections.Generic;
24+
using UnityEngine;
25+
26+
namespace IBM.Watson.DeveloperCloud.Examples
27+
{
28+
public class ExampleCallback : MonoBehaviour
29+
{
30+
private string _conversationUsername = "";
31+
private string _conversationPassword = "";
32+
private string _conversationUrl = "https://gateway.watsonplatform.net/conversation/api";
33+
private string _workspaceId = "";
34+
35+
private string _discoveryUsername = "";
36+
private string _discoveryPassword = "";
37+
private string _discoveryUrl = "https://gateway.watsonplatform.net/discovery/api";
38+
39+
void Start()
40+
{
41+
LogSystem.InstallDefaultReactors();
42+
43+
// Create conversation instance
44+
Credentials conversationCredentials = new Credentials(_conversationUsername, _conversationPassword, _conversationUrl);
45+
Conversation conversation = new Conversation(conversationCredentials);
46+
conversation.VersionDate = "2017-05-26";
47+
48+
// Create discovery instance
49+
Credentials discoveryCredentials = new Credentials(_discoveryUsername, _discoveryPassword, _discoveryUrl);
50+
Discovery discovery = new Discovery(discoveryCredentials);
51+
discovery.VersionDate = "2016-12-01";
52+
53+
// Call with generic callbacks
54+
conversation.Message(OnSuccess, OnFail, _workspaceId, "");
55+
discovery.GetEnvironments(OnSuccess, OnFail);
56+
57+
// Call with sepcific callbacks
58+
conversation.Message(OnMessage, OnMessageFail, _workspaceId, "");
59+
discovery.GetEnvironments(OnGetEnvironments, OnGetEnvironmentsFail);
60+
}
61+
62+
// Generic success callback
63+
private void OnSuccess<T>(T resp, Dictionary<string, object> customData)
64+
{
65+
Log.Debug("ExampleCallback.OnSuccess()", "Response received: {0}", customData["json"].ToString());
66+
}
67+
68+
// Generic fail callback
69+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
70+
{
71+
Log.Error("ExampleCallback.OnFail()", "Error received: {0}", error.ToString());
72+
}
73+
74+
// OnMessage callback
75+
private void OnMessage(object resp, Dictionary<string, object> customData)
76+
{
77+
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", customData["json"].ToString());
78+
}
79+
80+
// OnGetEnvironments callback
81+
private void OnGetEnvironments(GetEnvironmentsResponse resp, Dictionary<string, object> customData)
82+
{
83+
Log.Debug("ExampleCallback.OnGetEnvironments()", "Response received: {0}", customData["json"].ToString());
84+
}
85+
86+
// OnMessageFail callback
87+
private void OnMessageFail(RESTConnector.Error error, Dictionary<string, object> customData)
88+
{
89+
Log.Error("ExampleCallback.OnMessageFail()", "Error received: {0}", error.ToString());
90+
}
91+
92+
// OnGetEnvironmentsFail callback
93+
private void OnGetEnvironmentsFail(RESTConnector.Error error, Dictionary<string, object> customData)
94+
{
95+
Log.Error("ExampleCallback.OnGetEnvironmentsFail()", "Error received: {0}", error.ToString());
96+
}
97+
}
98+
}

Examples/ServiceExamples/Scripts/ExampleCallback.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Collections;
2323
using FullSerializer;
2424
using System.Collections.Generic;
25+
using IBM.Watson.DeveloperCloud.Connection;
2526

2627
public class ExampleConversation : MonoBehaviour
2728
{
@@ -54,7 +55,7 @@ void Start()
5455

5556
private IEnumerator Examples()
5657
{
57-
if (!_conversation.Message(OnMessage, _workspaceId, "hello"))
58+
if (!_conversation.Message(OnMessage, OnFail, _workspaceId, "hello"))
5859
Log.Debug("ExampleConversation.Message()", "Failed to message!");
5960

6061
while (_waitingForResponse)
@@ -103,13 +104,13 @@ private void AskQuestion()
103104
context = _context
104105
};
105106

106-
if (!_conversation.Message(OnMessage, _workspaceId, messageRequest))
107+
if (!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
107108
Log.Debug("ExampleConversation.AskQuestion()", "Failed to message!");
108109
}
109110

110-
private void OnMessage(object resp, string data)
111+
private void OnMessage(object resp, Dictionary<string, object> customData)
111112
{
112-
Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", data);
113+
Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString());
113114

114115
// Convert resp to fsdata
115116
fsData fsdata = null;
@@ -134,4 +135,9 @@ private void OnMessage(object resp, string data)
134135
Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
135136
_waitingForResponse = false;
136137
}
138+
139+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
140+
{
141+
Log.Error("ExampleConversation.OnFail()", "Error received: {0}", error.ToString());
142+
}
137143
}

0 commit comments

Comments
 (0)