Skip to content

Commit

Permalink
parse ErrorEvent in WebSocketHelper (#46)
Browse files Browse the repository at this point in the history
* parse ErrorEvent in WebSocketHelper

* Add integration test. Use verbatim string for Error event test.

Co-authored-by: bchavez <[email protected]>
  • Loading branch information
vslee and bchavez authored Dec 11, 2021
1 parent c435ed2 commit f8859ec
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Source/Coinbase.Pro/Models/WebSocketObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public class Event : Json
public string Type { get; set; }
}

public class ErrorEvent : Event
{
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("reason")]
public string Reason { get; set; }
}

public partial class SubscriptionsEvent : Event
{
[JsonProperty("channels")]
Expand Down Expand Up @@ -393,6 +401,4 @@ public partial class ActivateEvent : AuthenticatedEvent
[JsonProperty("private")]
public bool Private { get; set; }
}


}
4 changes: 4 additions & 0 deletions Source/Coinbase.Pro/WebSockets/WebSocketHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static bool TryParse(string json, out object parsed)
parsed = obj.ToObject<ActivateEvent>();
break;

case "error":
parsed = obj.ToObject<ErrorEvent>();
break;

default:
parsed = null;
return false;
Expand Down
28 changes: 28 additions & 0 deletions Source/Coinbase.Tests/IntegrationTests/WebSocketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,33 @@ public async Task connect_simple()
await Task.Delay(TimeSpan.FromMinutes(1));

}

[Test]
public async Task subscribe_with_error()
{
var result = await socket.ConnectAsync();
if (!result.Success)
{
throw new Exception("Connect error.");
}

socket.RawSocket.MessageReceived += RawSocket_MessageReceived;

var sub = new Subscription
{
ProductIds =
{
"ZZZ-YYY",
},
Channels =
{
"heartbeat",
}
};

await socket.SubscribeAsync(sub);

await Task.Delay(TimeSpan.FromMinutes(1));
}
}
}
17 changes: 17 additions & 0 deletions Source/Coinbase.Tests/WebSocketTests/EventModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,22 @@ public async Task activate()
var r = o as ActivateEvent;
r.Size.Should().Be(2);
}

[Test]
public async Task error()
{
var json = @"{
""type"":""error"",
""message"":""Failed to subscribe"",
""reason"":""ZZZ-YYY is not a valid product""
}";
WebSocketHelper.TryParse(json, out var o);

o.Should().BeOfType<ErrorEvent>();
o.Dump();
var r = o as ErrorEvent;
r.Message.Should().Be("Failed to subscribe");
}

}
}

0 comments on commit f8859ec

Please sign in to comment.