Skip to content

Commit

Permalink
send detailed error in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed May 28, 2024
1 parent d4b45c4 commit 6152c68
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 6 additions & 2 deletions Tests/WebSocketTestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ public class WebSocketTestClient
{
public readonly WebsocketClient Client;
public readonly List<BaseDto> ReceivedMessages = [];
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};

public WebSocketTestClient(string? url = null)
{
Client = url == null ? new WebsocketClient(new Uri("ws://localhost:" + (Environment.GetEnvironmentVariable("FULLSTACK_API_PORT") ?? "8181"))) : new WebsocketClient(new Uri(url));
Client.MessageReceived.Subscribe(msg =>
{
BaseDto baseDto = JsonSerializer.Deserialize<BaseDto>(msg.Text);
BaseDto baseDto = JsonSerializer.Deserialize<BaseDto>(msg.Text, JsonSerializerOptions);
Console.WriteLine("Received message: " + baseDto.eventType);

if (baseDto.eventType == "ServerSendsErrorMessage" || baseDto.eventType.Contains("ServerResponds") ||
baseDto.eventType.Contains("ServerRejects"))
{
var error = JsonSerializer.Deserialize<ServerSendsErrorMessage>(msg.Text);
var error = JsonSerializer.Deserialize<ServerSendsErrorMessage>(msg.Text, JsonSerializerOptions);
Console.WriteLine("Error: " + error!.Error);
}

Expand Down
3 changes: 1 addition & 2 deletions api/Events/Auth/Client/ClientWantsToLogIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio

});

/* socket.SendDto(new ServerSendsUserInfo
socket.SendDto(new ServerSendsUserInfo
{
GetUserDto = getUserDto
});
*/
}
}

9 changes: 5 additions & 4 deletions api/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public static void Handle(this Exception ex, IWebSocketConnection socket, string
_ => new ServerSendsErrorMessage { Error = message ?? ex.Message }
};
else
serverResponse = new ServerSendsErrorMessage
{
Error = "Something went wrong. Please try again later."
};
{
serverResponse = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Testing"
? new ServerSendsErrorMessage { Error = ex.Message }
: new ServerSendsErrorMessage { Error = "Something went wrong. Please try again later." };
}

socket.SendDto(serverResponse);
}
Expand Down
6 changes: 5 additions & 1 deletion api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static class Startup
];

private static readonly List<string?> NonProdEnvironments = ["Development", "Testing"];
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};

public static async Task Main(string[] args)
{
Expand Down Expand Up @@ -137,7 +141,7 @@ public static async Task<WebApplication> StartApi(string[] args)
try
{
// Check if the message contains a JWT token and if it is valid
var dto = JsonSerializer.Deserialize<BaseDtoWithJwt>(message, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
var dto = JsonSerializer.Deserialize<BaseDtoWithJwt>(message, JsonSerializerOptions);
if (dto is not null && PublicEvents.Contains(dto.eventType) == false)
{
if (dto.Jwt is null)
Expand Down

0 comments on commit 6152c68

Please sign in to comment.