Skip to content

Commit d393a0f

Browse files
authored
refactor: api error handling (#68)
1 parent 7ce655b commit d393a0f

28 files changed

+285
-274
lines changed

src/Adapters/Driving/Web/EndpointDefinitions/Analysis/DownloadAnalysisFileResultsEndpoint.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
using Microsoft.AspNetCore.Http.HttpResults;
33
using Microsoft.AspNetCore.Mvc;
44
using ScriptBee.Common.Web;
5-
using ScriptBee.Common.Web.Extensions;
65
using ScriptBee.Domain.Model.Analysis;
76
using ScriptBee.Domain.Model.Project;
87
using ScriptBee.Service.Project.Analysis;
98
using ScriptBee.UseCases.Project.Analysis;
9+
using ScriptBee.Web.Exceptions;
1010

1111
namespace ScriptBee.Web.EndpointDefinitions.Analysis;
1212

@@ -58,20 +58,8 @@ private static async Task<DownloadResult> DownloadIndividualFile(
5858
MediaTypeNames.Application.Octet,
5959
namedStream.Name
6060
),
61-
error =>
62-
TypedResults.NotFound(
63-
context.ToProblemDetails(
64-
"Analysis Not Found",
65-
$"An analysis with the ID '{error.Id.Value}' does not exists."
66-
)
67-
),
68-
error =>
69-
TypedResults.NotFound(
70-
context.ToProblemDetails(
71-
"Result Not Found",
72-
$"An analysis result with the ID '{error.Id.Value}' does not exists."
73-
)
74-
)
61+
error => error.ToProblem(context),
62+
error =>error.ToProblem(context)
7563
);
7664
}
7765

@@ -96,13 +84,7 @@ private static async Task<DownloadResult> DownloadAllFiles(
9684
MediaTypeNames.Application.Octet,
9785
namedStream.Name
9886
),
99-
error =>
100-
TypedResults.NotFound(
101-
context.ToProblemDetails(
102-
"Analysis Not Found",
103-
$"An analysis with the ID '{error.Id.Value}' does not exists."
104-
)
105-
)
87+
error => error.ToProblem(context)
10688
);
10789
}
10890
}

src/Adapters/Driving/Web/EndpointDefinitions/Analysis/GetAnalysisResultsEndpoint.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.Analysis;
65
using ScriptBee.Domain.Model.Project;
76
using ScriptBee.Service.Project.Analysis;
87
using ScriptBee.UseCases.Project.Analysis;
98
using ScriptBee.Web.EndpointDefinitions.Analysis.Contracts;
9+
using ScriptBee.Web.Exceptions;
1010

1111
namespace ScriptBee.Web.EndpointDefinitions.Analysis;
1212

@@ -51,13 +51,7 @@ private static async Task<
5151

5252
return result.Match<Results<Ok<WebGetAnalysisResultConsole>, NotFound<ProblemDetails>>>(
5353
content => TypedResults.Ok(new WebGetAnalysisResultConsole(content)),
54-
error =>
55-
TypedResults.NotFound(
56-
context.ToProblemDetails(
57-
"Analysis Not Found",
58-
$"An analysis with the ID '{error.Id.Value}' does not exists."
59-
)
60-
)
54+
error =>error.ToProblem(context)
6155
);
6256
}
6357

@@ -79,13 +73,7 @@ private static async Task<
7973

8074
return result.Match<Results<Ok<WebGetAnalysisResultRunErrors>, NotFound<ProblemDetails>>>(
8175
results => TypedResults.Ok(WebGetAnalysisResultRunErrors.Map(results)),
82-
error =>
83-
TypedResults.NotFound(
84-
context.ToProblemDetails(
85-
"Analysis Not Found",
86-
$"An analysis with the ID '{error.Id.Value}' does not exists."
87-
)
88-
)
76+
error => error.ToProblem(context)
8977
);
9078
}
9179

@@ -107,13 +95,7 @@ private static async Task<
10795

10896
return result.Match<Results<Ok<WebGetAnalysisResultFileList>, NotFound<ProblemDetails>>>(
10997
files => TypedResults.Ok(WebGetAnalysisResultFileList.Map(files)),
110-
error =>
111-
TypedResults.NotFound(
112-
context.ToProblemDetails(
113-
"Analysis Not Found",
114-
$"An analysis with the ID '{error.Id.Value}' does not exists."
115-
)
116-
)
98+
error =>error.ToProblem(context)
11799
);
118100
}
119101
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextClearEndpoint.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.Instance;
65
using ScriptBee.Domain.Model.Project;
76
using ScriptBee.Service.Project.Context;
87
using ScriptBee.UseCases.Project.Context;
8+
using ScriptBee.Web.Exceptions;
99

1010
namespace ScriptBee.Web.EndpointDefinitions.Context;
1111

@@ -37,13 +37,7 @@ CancellationToken cancellationToken
3737

3838
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
3939
_ => TypedResults.NoContent(),
40-
error =>
41-
TypedResults.NotFound(
42-
context.ToProblemDetails(
43-
"Instance Not Found",
44-
$"An instance with id '{error.InstanceId}' is not allocated."
45-
)
46-
)
40+
error => error.ToProblem(context)
4741
);
4842
}
4943
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextLinkEndpoint.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Common.Web.Validation;
65
using ScriptBee.Domain.Model.Instance;
76
using ScriptBee.Domain.Model.Project;
87
using ScriptBee.Service.Project.Context;
98
using ScriptBee.UseCases.Project.Context;
109
using ScriptBee.Web.EndpointDefinitions.Context.Contracts;
10+
using ScriptBee.Web.Exceptions;
1111

1212
namespace ScriptBee.Web.EndpointDefinitions.Context;
1313

@@ -42,20 +42,8 @@ CancellationToken cancellationToken
4242

4343
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
4444
_ => TypedResults.NoContent(),
45-
error =>
46-
TypedResults.NotFound(
47-
context.ToProblemDetails(
48-
"Project Not Found",
49-
$"A project with the ID '{error.Id.Value}' does not exists."
50-
)
51-
),
52-
error =>
53-
TypedResults.NotFound(
54-
context.ToProblemDetails(
55-
"Instance Not Found",
56-
$"An instance with id '{error.InstanceId}' is not allocated."
57-
)
58-
)
45+
error => error.ToProblem(context),
46+
error =>error.ToProblem(context)
5947
);
6048
}
6149
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextLoadEndpoint.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Common.Web.Validation;
65
using ScriptBee.Domain.Model.Instance;
76
using ScriptBee.Domain.Model.Project;
87
using ScriptBee.Service.Project.Context;
98
using ScriptBee.UseCases.Project.Context;
109
using ScriptBee.Web.EndpointDefinitions.Context.Contracts;
10+
using ScriptBee.Web.Exceptions;
1111

1212
namespace ScriptBee.Web.EndpointDefinitions.Context;
1313

@@ -42,20 +42,8 @@ CancellationToken cancellationToken
4242

4343
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
4444
_ => TypedResults.NoContent(),
45-
error =>
46-
TypedResults.NotFound(
47-
context.ToProblemDetails(
48-
"Project Not Found",
49-
$"A project with the ID '{error.Id.Value}' does not exists."
50-
)
51-
),
52-
error =>
53-
TypedResults.NotFound(
54-
context.ToProblemDetails(
55-
"Instance Not Found",
56-
$"An instance with id '{error.InstanceId}' is not allocated."
57-
)
58-
)
45+
error => error.ToProblem(context),
46+
error => error.ToProblem(context)
5947
);
6048
}
6149
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextReloadEndpoint.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.Instance;
65
using ScriptBee.Domain.Model.Project;
76
using ScriptBee.Service.Project.Context;
87
using ScriptBee.UseCases.Project.Context;
8+
using ScriptBee.Web.Exceptions;
99

1010
namespace ScriptBee.Web.EndpointDefinitions.Context;
1111

@@ -40,20 +40,8 @@ CancellationToken cancellationToken
4040

4141
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
4242
_ => TypedResults.NoContent(),
43-
error =>
44-
TypedResults.NotFound(
45-
context.ToProblemDetails(
46-
"Project Not Found",
47-
$"A project with the ID '{error.Id.Value}' does not exists."
48-
)
49-
),
50-
error =>
51-
TypedResults.NotFound(
52-
context.ToProblemDetails(
53-
"Instance Not Found",
54-
$"An instance with id '{error.InstanceId}' is not allocated."
55-
)
56-
)
43+
error => error.ToProblem(context),
44+
error =>error.ToProblem(context)
5745
);
5846
}
5947
}

src/Adapters/Driving/Web/EndpointDefinitions/Instances/AddProjectInstanceEndpoint.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.Project;
65
using ScriptBee.Service.Project.Analysis;
76
using ScriptBee.UseCases.Project.Analysis;
87
using ScriptBee.Web.EndpointDefinitions.Instances.Contracts;
8+
using ScriptBee.Web.Exceptions;
99

1010
namespace ScriptBee.Web.EndpointDefinitions.Instances;
1111

@@ -39,13 +39,7 @@ private static async Task<AllocateInstanceType> AllocateInstance(
3939
$"/api/projects/{projectId}/instances/{instanceInfo.Id}",
4040
WebProjectInstance.Map(instanceInfo)
4141
),
42-
error =>
43-
TypedResults.NotFound(
44-
context.ToProblemDetails(
45-
"Project Not Found",
46-
$"A project with the ID '{error.Id.Value}' does not exists."
47-
)
48-
)
42+
error => error.ToProblem(context)
4943
);
5044
}
5145
}

src/Adapters/Driving/Web/EndpointDefinitions/Loaders/UploadLoaderFilesEndpoint.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.File;
65
using ScriptBee.Domain.Model.Project;
76
using ScriptBee.Service.Project.Files;
87
using ScriptBee.UseCases.Project.Files;
98
using ScriptBee.Web.EndpointDefinitions.Loaders.Contracts;
9+
using ScriptBee.Web.Exceptions;
1010

1111
namespace ScriptBee.Web.EndpointDefinitions.Loaders;
1212

@@ -54,13 +54,7 @@ private static async Task<UploadResult> UploadLoaderFiles(
5454
TypedResults.Ok(
5555
new WebUploadLoaderFilesResponse(loaderId, fileData.Select(f => f.Name))
5656
),
57-
error =>
58-
TypedResults.NotFound(
59-
context.ToProblemDetails(
60-
"Project Not Found",
61-
$"A project with the ID '{error.Id.Value}' does not exists."
62-
)
63-
)
57+
error =>error.ToProblem(context)
6458
);
6559
}
6660
}

src/Adapters/Driving/Web/EndpointDefinitions/Project/CreateProjectEndpoint.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Common.Web.Validation;
65
using ScriptBee.Service.Project;
76
using ScriptBee.UseCases.Project;
87
using ScriptBee.Web.EndpointDefinitions.Project.Contracts;
8+
using ScriptBee.Web.Exceptions;
99

1010
namespace ScriptBee.Web.EndpointDefinitions.Project;
1111

@@ -39,13 +39,7 @@ private static async Task<
3939
var response = WebCreateProjectResponse.Map(projectDetails);
4040
return TypedResults.Created($"/api/projects/{response.Id}", response);
4141
},
42-
error =>
43-
TypedResults.Conflict(
44-
context.ToProblemDetails(
45-
"Project ID Already In Use",
46-
$"A project with the ID '{error.Id.Value}' already exists. Use a unique Project ID or update the existing project."
47-
)
48-
)
42+
error => error.ToProblem(context)
4943
);
5044
}
5145
}

src/Adapters/Driving/Web/EndpointDefinitions/Project/GetProjectsEndpoint.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web;
4-
using ScriptBee.Common.Web.Extensions;
54
using ScriptBee.Domain.Model.Project;
65
using ScriptBee.Service.Project;
76
using ScriptBee.UseCases.Project;
87
using ScriptBee.Web.EndpointDefinitions.Project.Contracts;
8+
using ScriptBee.Web.Exceptions;
99

1010
namespace ScriptBee.Web.EndpointDefinitions.Project;
1111

@@ -46,13 +46,7 @@ private static async Task<
4646

4747
return result.Match<Results<Ok<WebGetProjectDetailsResponse>, NotFound<ProblemDetails>>>(
4848
projectDetails => TypedResults.Ok(WebGetProjectDetailsResponse.Map(projectDetails)),
49-
error =>
50-
TypedResults.NotFound(
51-
context.ToProblemDetails(
52-
"Project Not Found",
53-
$"A project with the ID '{error.Id.Value}' does not exists."
54-
)
55-
)
49+
error => error.ToProblem(context)
5650
);
5751
}
5852
}

0 commit comments

Comments
 (0)