Commit 0d5d133 1 parent 726defb commit 0d5d133 Copy full SHA for 0d5d133
File tree 4 files changed +32
-9
lines changed
4 files changed +32
-9
lines changed Original file line number Diff line number Diff line change @@ -33,7 +33,14 @@ public ActionResult<CalculatorInfo> GetInfo()
33
33
[ HttpGet ( "calculation" ) ]
34
34
public async Task < ActionResult < TCalculation > > GetCalculation ( [ FromQuery ] TScore score )
35
35
{
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
+ }
37
44
}
38
45
39
46
/// <summary>
@@ -43,7 +50,14 @@ public async Task<ActionResult<TCalculation>> GetCalculation([FromQuery] TScore
43
50
[ Consumes ( "application/json" ) ]
44
51
public async Task < ActionResult < TCalculation [ ] > > GetCalculationBatch ( [ FromBody ] TScore [ ] scores )
45
52
{
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
+ }
47
61
}
48
62
}
49
63
}
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace Difficalcy . Services
4
+ {
5
+ public class BeatmapNotFoundException ( string beatmapId ) : Exception ( $ "Beatmap { beatmapId } not found")
6
+ {
7
+ }
8
+ }
Original file line number Diff line number Diff line change 1
- using System ;
2
1
using System . IO ;
3
2
using System . Reflection ;
4
3
using System . Threading . Tasks ;
5
- using Microsoft . AspNetCore . Http ;
6
4
7
5
namespace Difficalcy . Services
8
6
{
@@ -11,7 +9,7 @@ public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider
11
9
public Task EnsureBeatmap ( string beatmapId )
12
10
{
13
11
var resourceName = GetResourceName ( beatmapId ) ;
14
- _ = ResourceAssembly . GetManifestResourceInfo ( resourceName ) ?? throw new BadHttpRequestException ( $ "Beatmap not found: { beatmapId } " ) ;
12
+ _ = ResourceAssembly . GetManifestResourceInfo ( resourceName ) ?? throw new BeatmapNotFoundException ( beatmapId ) ;
15
13
return Task . CompletedTask ;
16
14
}
17
15
Original file line number Diff line number Diff line change 1
1
using System . IO ;
2
2
using System . Net . Http ;
3
3
using System . Threading . Tasks ;
4
- using Microsoft . AspNetCore . Http ;
5
4
using Microsoft . Extensions . Configuration ;
6
5
7
6
namespace Difficalcy . Services
@@ -18,16 +17,20 @@ public async Task EnsureBeatmap(string beatmapId)
18
17
if ( ! File . Exists ( beatmapPath ) )
19
18
{
20
19
if ( _downloadMissingBeatmaps != "true" )
21
- throw new BadHttpRequestException ( "Beatmap not found" ) ;
20
+ throw new BeatmapNotFoundException ( beatmapId ) ;
22
21
23
22
using var response = await _httpClient . GetAsync ( $ "https://osu.ppy.sh/osu/{ beatmapId } ") ;
24
23
if ( ! response . IsSuccessStatusCode || response . Content . Headers . ContentLength == 0 )
25
- throw new BadHttpRequestException ( "Beatmap not found" ) ;
24
+ throw new BeatmapNotFoundException ( beatmapId ) ;
26
25
27
26
using var fs = new FileStream ( beatmapPath , FileMode . CreateNew ) ;
28
27
await response . Content . CopyToAsync ( fs ) ;
29
28
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
+ }
31
34
}
32
35
}
33
36
You can’t perform that action at this time.
0 commit comments