Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Sep 23, 2024
1 parent a9e5a30 commit 44d6bdb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public async Task<IActionResult> UploadHtmlAsync(ControllerBase controller, Canc
var html = await streamReader.ReadToEndAsync(ct);
var (valid, version, crashReportModel) = ParseHtml(html);
if (!valid)
{
_logger.LogWarning("Invalid HTML");
return controller.StatusCode(StatusCodes.Status500InternalServerError);
}

if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReportModel!.Id, ct) is { } idEntity)
return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}");
Expand Down
3 changes: 3 additions & 0 deletions src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public async Task<IActionResult> UploadJsonAsync(ControllerBase controller, Canc
controller.Request.Body = await _gZipCompressor.DecompressAsync(controller.Request.Body, ct);

if (await controller.HttpContext.Request.ReadFromJsonAsync<CrashReportUploadBodyV13>(_jsonSerializerOptions, ct) is not { CrashReport: { } crashReport, LogSources: { } logSources })
{
_logger.LogWarning("Failed to read JSON body");
return controller.StatusCode(StatusCodes.Status500InternalServerError);
}

if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReport.Id, ct) is { } idEntity)
return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}");
Expand Down
3 changes: 3 additions & 0 deletions src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public async Task<IActionResult> UploadHtmlAsync(ControllerBase controller, Canc
var html = await streamReader.ReadToEndAsync(ct);
var (valid, version, crashReportModel) = ParseHtml(html);
if (!valid)
{
_logger.LogWarning("Invalid HTML");
return controller.StatusCode(StatusCodes.Status500InternalServerError);
}

if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReportModel!.Id, ct) is { } idEntity)
return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}");
Expand Down
3 changes: 3 additions & 0 deletions src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public async Task<IActionResult> UploadJsonAsync(ControllerBase controller, Canc
controller.Request.Body = await _gZipCompressor.DecompressAsync(controller.Request.Body, ct);

if (await controller.HttpContext.Request.ReadFromJsonAsync<CrashReportUploadBodyV14>(_jsonSerializerOptions, ct) is not { CrashReport: { } crashReport, LogSources: { } logSources })
{
_logger.LogWarning("Failed to read JSON body");
return controller.StatusCode(StatusCodes.Status500InternalServerError);
}

if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReport.Id, ct) is { } idEntity)
return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}");
Expand Down
13 changes: 12 additions & 1 deletion src/BUTR.CrashReport.Server/Controllers/CrashUploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public CrashUploadController(
public Task<IActionResult> CrashUploadAsync(CancellationToken ct)
{
if (Request.ContentLength is not { } contentLength || contentLength < _options.MinContentLength || contentLength > _options.MaxContentLength)
{
_logger.LogWarning("Content length is invalid: {ContentLength}", Request.ContentLength);
return Task.FromResult<IActionResult>(StatusCode(StatusCodes.Status500InternalServerError));
}

switch (Request.ContentType)
{
Expand All @@ -67,14 +70,18 @@ public Task<IActionResult> CrashUploadAsync(CancellationToken ct)

private async Task<IActionResult> UploadJsonAsync(CancellationToken ct)
{
switch (Request.Headers["CrashReportVersion"])
var version = Request.Headers["CrashReportVersion"].ToString();
switch (version)
{
case "13":
return await _jsonHandlerV13.UploadJsonAsync(this, ct);
case "14":
return await _jsonHandlerV14.UploadJsonAsync(this, ct);
default:
{
_logger.LogWarning("Crash report version is invalid: {CrashReportVersion}", version);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
}

Expand Down Expand Up @@ -109,13 +116,17 @@ static bool TryParseVersion(string content, out byte version)
using var streamReader = new StreamReader(Request.Body);
var html = await streamReader.ReadToEndAsync(ct);
if (!TryParseVersion(html, out var version))
{
_logger.LogWarning("Failed to parse html crash report version");
return StatusCode(StatusCodes.Status500InternalServerError);
}

if (version <= 13)
return await _htmlHandlerV13.UploadHtmlAsync(this, ct);
if (version == 14)
return await _htmlHandlerV14.UploadHtmlAsync(this, ct);

_logger.LogWarning("Crash report version is invalid: {CrashReportVersion}", version);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}

0 comments on commit 44d6bdb

Please sign in to comment.