Skip to content

Commit 0407946

Browse files
committed
Custom exception, cleanup
1 parent 496c8a5 commit 0407946

File tree

8 files changed

+39
-11
lines changed

8 files changed

+39
-11
lines changed

src/QAToolKit.Source.Swagger.Test/Fixtures/BicycleApi/Get/GetBicycleByIdResponse.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using QAToolKit.Core.Models;
22
using QAToolKit.Source.Swagger.Test.Fixtures.BicycleApi.Get.Helpers;
3-
using QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Get.Helpers;
43
using System.Collections.Generic;
54

65
namespace QAToolKit.Source.Swagger.Test.Fixtures.BicycleApi.Get

src/QAToolKit.Source.Swagger.Test/SwaggerFileSourceIntegrationTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Logging;
2+
using QAToolKit.Source.Swagger.Exceptions;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -40,7 +41,7 @@ public async void SwaggerFileSourceWithoutOptionsTest_Fails()
4041
{
4142
var fileSource = new SwaggerFileSource();
4243

43-
var exception = await Assert.ThrowsAsync<Exception>(async () => await fileSource.Load(new List<FileInfo>() {
44+
var exception = await Assert.ThrowsAsync<QAToolKitSwaggerException>(async () => await fileSource.Load(new List<FileInfo>() {
4445
new FileInfo("Assets/swagger-pets-test.json")
4546
}));
4647

src/QAToolKit.Source.Swagger.Test/SwaggerTests/BicycleApi/Get/SwaggerProcessorGetBicycleByIdTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
using QAToolKit.Core.Models;
55
using QAToolKit.Source.Swagger.Test.Fixtures.BicycleApi.Get;
66
using QAToolKit.Source.Swagger.Test.Fixtures.BicycleApi.Get.Helpers;
7-
using QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Get;
8-
using QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Post;
97
using System;
10-
using System.Collections.Generic;
11-
using System.IO;
128
using System.Linq;
139
using System.Net.Http;
1410
using System.Threading.Tasks;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace QAToolKit.Source.Swagger.Exceptions
5+
{
6+
internal class QAToolKitSwaggerException : Exception
7+
{
8+
public QAToolKitSwaggerException(string message) : base(message)
9+
{
10+
}
11+
12+
public QAToolKitSwaggerException(string message, Exception innerException) : base(message, innerException)
13+
{
14+
}
15+
16+
protected QAToolKitSwaggerException(SerializationInfo info, StreamingContext context) : base(info, context)
17+
{
18+
}
19+
}
20+
}

src/QAToolKit.Source.Swagger/SwaggerOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class SwaggerOptions
4747
/// <returns></returns>
4848
public SwaggerOptions AddBasicAuthentication(string userName, string password)
4949
{
50+
if (string.IsNullOrEmpty(userName))
51+
throw new ArgumentException(nameof(userName));
52+
if (string.IsNullOrEmpty(password))
53+
throw new ArgumentException(nameof(password));
54+
5055
UseBasicAuth = true;
5156
UserName = userName;
5257
Password = password;
@@ -60,6 +65,9 @@ public SwaggerOptions AddBasicAuthentication(string userName, string password)
6065
/// <returns></returns>
6166
public SwaggerOptions AddRequestFilters(RequestFilter requestFilter)
6267
{
68+
if (requestFilter == null)
69+
throw new ArgumentException(nameof(requestFilter));
70+
6371
UseRequestFilter = true;
6472
RequestFilter = requestFilter;
6573
return this;
@@ -72,6 +80,9 @@ public SwaggerOptions AddRequestFilters(RequestFilter requestFilter)
7280
/// <returns></returns>
7381
public SwaggerOptions AddBaseUrl(Uri baseUrl)
7482
{
83+
if (baseUrl == null)
84+
throw new ArgumentException(nameof(baseUrl));
85+
7586
BaseUrl = baseUrl;
7687
return this;
7788
}

src/QAToolKit.Source.Swagger/SwaggerProcessor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.OpenApi.Models;
33
using Microsoft.OpenApi.Writers;
44
using QAToolKit.Core.Models;
5+
using QAToolKit.Source.Swagger.Exceptions;
56
using System;
67
using System.Collections.Generic;
78
using System.Data;
@@ -50,7 +51,7 @@ public IList<HttpRequest> MapFromOpenApiDocument(Uri baseUri, OpenApiDocument op
5051
{
5152
if (baseUri == null)
5253
{
53-
throw new Exception("Swagger from file source needs BaseUrl defined. Inject baseUrl with AddBaseUrl in your SwaggerSource instantiation.");
54+
throw new QAToolKitSwaggerException("Swagger from file source needs BaseUrl defined. Inject baseUrl with AddBaseUrl in your SwaggerSource instantiation.");
5455
}
5556

5657
baseUri = new Uri(baseUri, tempUri);
@@ -192,7 +193,7 @@ private HttpMethod GetHttpMethod(KeyValuePair<OperationType, OpenApiOperation> o
192193
return HttpMethod.Patch;
193194
#endif
194195
default:
195-
throw new Exception("HttpMethod invalid.");
196+
throw new QAToolKitSwaggerException("HttpMethod invalid.");
196197
}
197198
}
198199

@@ -225,7 +226,7 @@ private HttpMethod GetHttpMethod(KeyValuePair<OperationType, OpenApiOperation> o
225226
case "default":
226227
return null;
227228
default:
228-
throw new Exception("HttpStatusCode not found.");
229+
throw new QAToolKitSwaggerException("HttpStatusCode not found.");
229230
}
230231
}
231232

src/QAToolKit.Source.Swagger/SwaggerRequestFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using QAToolKit.Core.Models;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45

@@ -17,7 +18,7 @@ public class SwaggerRequestFilter
1718
/// <param name="requests"></param>
1819
public SwaggerRequestFilter(IList<HttpRequest> requests)
1920
{
20-
_requests = requests;
21+
_requests = requests ?? throw new ArgumentNullException(nameof(requests));
2122
}
2223
/// <summary>
2324
/// Filter out the requests by the specified filters

src/QAToolKit.Source.Swagger/SwaggerUrlSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.OpenApi.Readers;
22
using Microsoft.OpenApi.Writers;
3-
using Newtonsoft.Json;
43
using QAToolKit.Core.Interfaces;
54
using QAToolKit.Core.Models;
65
using System;

0 commit comments

Comments
 (0)