Open JCKodel opened 5 years ago
Using the latest source code, the error remains. Debugging shows that Emitter responds to the keygen request this json:
{"status":401,"message":"the security key provided is not authorized to perform this operation"}
At KeygenResponse.cs, line 47, there is no error handling code written (it assumes the response will always have a "key" key, hence the exception):
public static KeygenResponse FromJson(string json)
{
var map = JsonSerializer.DeserializeString(json) as Hashtable;
var response = new KeygenResponse();
response.Key = (string)map["key"];
response.Channel = (string)map["channel"];
response.Status = Convert.ToInt32(map["status"].ToString());
return response;
}
The safest code would be:
public static KeygenResponse FromJson(string json)
{
var map = JsonSerializer.DeserializeString(json) as Hashtable;
var response = new KeygenResponse();
object key;
object channel;
object message;
object status;
map.TryGetValue("key", out key);
map.TryGetValue("channel", out channel);
map.TryGetValue("message", out message);
map.TryGetValue("status", out status);
if(key != null) response.Key = (string)key;
if(channel != null) response.Channel = (string)channel;
if(status != null) response.Status = Convert.ToInt32(status);
if(response.Status > 299 && message != null)
{
throw new EmitterException((EmitterEventCode)response.Status, (string)message);
}
return response;
}
That would give me a more meaningful error message (although is my first try with Emitter, I have no clue what is going on):
Error: Emitter.EmitterException: the security key provided is not authorized to perform this operation
at Emitter.Messages.KeygenResponse.FromJson(String json) in C:\Temp\csharp-master\Emitter\Messages\KeygenResponse.cs:line 70
at Emitter.Messages.KeygenResponse.FromBinary(Byte[] message) in C:\Temp\csharp-master\Emitter\Messages\KeygenResponse.cs:line 80
at Emitter.Connection.OnMessageReceived(Object sender, MqttMsgPublishEventArgs e) in C:\Temp\csharp-master\Emitter\Emitter.cs:line 430
While running this code (using the Emitter nuget on a .net core console project):
this error is thrown: