Skip to content

Commit

Permalink
Merge pull request ppy#216 from smoogipoo/fix-exception-type
Browse files Browse the repository at this point in the history
Change type of some exceptions so they're logged
  • Loading branch information
bdach authored Feb 1, 2024
2 parents 1e6947b + eec735b commit 54f667d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Moq;
using osu.Game.Online.Multiplayer;
using osu.Server.Spectator.Entities;
using osu.Server.Spectator.Hubs.Spectator;
using Xunit;
Expand Down Expand Up @@ -120,7 +119,7 @@ public async Task TestConcurrencyBlocked()
var firstInvocationContext = new HubInvocationContext(firstContextMock.Object, serviceProviderMock.Object, hubMock.Object,
typeof(SpectatorHub).GetMethod(nameof(SpectatorHub.StartWatchingUser))!, new object[] { 1234 });
// should throw.
await Assert.ThrowsAsync<InvalidStateException>(() => filter.InvokeMethodAsync(firstInvocationContext, _ => new ValueTask<object?>(new object())).AsTask());
await Assert.ThrowsAsync<InvalidOperationException>(() => filter.InvokeMethodAsync(firstInvocationContext, _ => new ValueTask<object?>(new object())).AsTask());
}

[Fact]
Expand Down
3 changes: 1 addition & 2 deletions osu.Server.Spectator/ConcurrentConnectionLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Logging;
using osu.Game.Online;
using osu.Game.Online.Multiplayer;
using osu.Server.Spectator.Entities;
using osu.Server.Spectator.Extensions;
using osu.Server.Spectator.Hubs;
Expand Down Expand Up @@ -102,7 +101,7 @@ private static void log(HubLifetimeContext context, string message)
bool connectionIsValid = tokenIdMatches && hubRegistered && connectionIdMatches;

if (!connectionIsValid)
throw new InvalidStateException($"State is not valid for this connection, context: {LoggingHubFilter.GetMethodCallDisplayString(invocationContext)})");
throw new InvalidOperationException($"State is not valid for this connection, context: {LoggingHubFilter.GetMethodCallDisplayString(invocationContext)})");
}

return await next(invocationContext);
Expand Down
6 changes: 3 additions & 3 deletions osu.Server.Spectator/Hubs/Multiplayer/MultiplayerHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task<MultiplayerRoom> JoinRoomWithPassword(long roomId, string pass
/// It should only be called by the room's host, before any other user has joined (and will throw if not).
/// </summary>
/// <param name="roomId">The proposed room ID.</param>
/// <exception cref="InvalidStateException">If anything is wrong with this request.</exception>
/// <exception cref="InvalidOperationException">If anything is wrong with this request.</exception>
private async Task<ServerMultiplayerRoom> retrieveRoom(long roomId)
{
Log($"Retrieving room {roomId} from database");
Expand All @@ -176,13 +176,13 @@ private async Task<ServerMultiplayerRoom> retrieveRoom(long roomId)
var databaseRoom = await db.GetRoomAsync(roomId);

if (databaseRoom == null)
throw new InvalidStateException("Specified match does not exist.");
throw new InvalidOperationException("Specified match does not exist.");

if (databaseRoom.ends_at != null && databaseRoom.ends_at < DateTimeOffset.Now)
throw new InvalidStateException("Match has already ended.");

if (databaseRoom.user_id != Context.GetUserId())
throw new InvalidStateException("Non-host is attempting to join match before host");
throw new InvalidOperationException("Non-host is attempting to join match before host");

var room = new ServerMultiplayerRoom(roomId, HubContext)
{
Expand Down
3 changes: 1 addition & 2 deletions osu.Server.Spectator/Hubs/StatefulUserHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Caching.Distributed;
using osu.Game.Online;
using osu.Game.Online.Multiplayer;
using osu.Server.Spectator.Entities;
using osu.Server.Spectator.Extensions;

Expand Down Expand Up @@ -113,7 +112,7 @@ protected async Task<ItemUsage<TUserState>> GetOrCreateLocalUserState()
if (usage.Item != null && usage.Item.ConnectionId != Context.ConnectionId)
{
usage.Dispose();
throw new InvalidStateException("State is not valid for this connection");
throw new InvalidOperationException("State is not valid for this connection");
}

return usage;
Expand Down

0 comments on commit 54f667d

Please sign in to comment.