Skip to content

Commit 0d5d133

Browse files
committed
Use custom exception for missing beatmaps
1 parent 726defb commit 0d5d133

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

Difficalcy/Controllers/CalculatorController.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public ActionResult<CalculatorInfo> GetInfo()
3333
[HttpGet("calculation")]
3434
public async Task<ActionResult<TCalculation>> GetCalculation([FromQuery] TScore score)
3535
{
36-
return Ok(await calculatorService.GetCalculation(score));
36+
try
37+
{
38+
return Ok(await calculatorService.GetCalculation(score));
39+
}
40+
catch (BeatmapNotFoundException e)
41+
{
42+
return BadRequest(new { error = e.Message });
43+
}
3744
}
3845

3946
/// <summary>
@@ -43,7 +50,14 @@ public async Task<ActionResult<TCalculation>> GetCalculation([FromQuery] TScore
4350
[Consumes("application/json")]
4451
public async Task<ActionResult<TCalculation[]>> GetCalculationBatch([FromBody] TScore[] scores)
4552
{
46-
return Ok(await Task.WhenAll(scores.Select(score => calculatorService.GetCalculation(score))));
53+
try
54+
{
55+
return Ok(await Task.WhenAll(scores.Select(calculatorService.GetCalculation)));
56+
}
57+
catch (BeatmapNotFoundException e)
58+
{
59+
return BadRequest(new { error = e.Message });
60+
}
4761
}
4862
}
4963
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace Difficalcy.Services
4+
{
5+
public class BeatmapNotFoundException(string beatmapId) : Exception($"Beatmap {beatmapId} not found")
6+
{
7+
}
8+
}

Difficalcy/Services/TestBeatmapProvider.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
21
using System.IO;
32
using System.Reflection;
43
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Http;
64

75
namespace Difficalcy.Services
86
{
@@ -11,7 +9,7 @@ public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider
119
public Task EnsureBeatmap(string beatmapId)
1210
{
1311
var resourceName = GetResourceName(beatmapId);
14-
_ = ResourceAssembly.GetManifestResourceInfo(resourceName) ?? throw new BadHttpRequestException($"Beatmap not found: {beatmapId}");
12+
_ = ResourceAssembly.GetManifestResourceInfo(resourceName) ?? throw new BeatmapNotFoundException(beatmapId);
1513
return Task.CompletedTask;
1614
}
1715

Difficalcy/Services/WebBeatmapProvider.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.IO;
22
using System.Net.Http;
33
using System.Threading.Tasks;
4-
using Microsoft.AspNetCore.Http;
54
using Microsoft.Extensions.Configuration;
65

76
namespace Difficalcy.Services
@@ -18,16 +17,20 @@ public async Task EnsureBeatmap(string beatmapId)
1817
if (!File.Exists(beatmapPath))
1918
{
2019
if (_downloadMissingBeatmaps != "true")
21-
throw new BadHttpRequestException("Beatmap not found");
20+
throw new BeatmapNotFoundException(beatmapId);
2221

2322
using var response = await _httpClient.GetAsync($"https://osu.ppy.sh/osu/{beatmapId}");
2423
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength == 0)
25-
throw new BadHttpRequestException("Beatmap not found");
24+
throw new BeatmapNotFoundException(beatmapId);
2625

2726
using var fs = new FileStream(beatmapPath, FileMode.CreateNew);
2827
await response.Content.CopyToAsync(fs);
2928
if (fs.Length == 0)
30-
throw new BadHttpRequestException("Beatmap not found");
29+
{
30+
fs.Close();
31+
File.Delete(beatmapPath);
32+
throw new BeatmapNotFoundException(beatmapId);
33+
}
3134
}
3235
}
3336

0 commit comments

Comments
 (0)