Skip to content

Commit

Permalink
Merge pull request #1388 from SimonCropp/cleanup-exception-handling
Browse files Browse the repository at this point in the history
cleanup exception handling
  • Loading branch information
baywet authored Oct 5, 2023
2 parents 22c7a3d + bb0f507 commit eb97519
Show file tree
Hide file tree
Showing 54 changed files with 125 additions and 431 deletions.
7 changes: 1 addition & 6 deletions src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ public class DisplayAttribute : Attribute
/// <param name="name">The display name.</param>
public DisplayAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(name));
}

Name = name;
Name = Utils.CheckArgumentNullOrEmpty(name);
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if !NETCOREAPP3_1_OR_GREATER
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.CompilerServices;

[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Parameter)]
sealed class CallerArgumentExpressionAttribute :
Attribute
{
public CallerArgumentExpressionAttribute(string parameterName) =>
ParameterName = parameterName;

public string ParameterName { get; }
}
#endif
98 changes: 0 additions & 98 deletions src/Microsoft.OpenApi/Error.cs

This file was deleted.

5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Expressions/BodyExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public BodyExpression()
public BodyExpression(JsonPointer pointer)
: base(pointer?.ToString())
{
if (pointer == null)
{
throw Error.ArgumentNull(nameof(pointer));
}
Utils.CheckArgumentNull(pointer);
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Expressions/HeaderExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public class HeaderExpression : SourceExpression
public HeaderExpression(string token)
: base(token)
{
if (string.IsNullOrWhiteSpace(token))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(token));
}
Utils.CheckArgumentNullOrEmpty(token);
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Expressions/PathExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public sealed class PathExpression : SourceExpression
public PathExpression(string name)
: base(name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(name));
}
Utils.CheckArgumentNullOrEmpty(name);
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Expressions/QueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public sealed class QueryExpression : SourceExpression
public QueryExpression(string name)
: base(name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(name));
}
Utils.CheckArgumentNullOrEmpty(name);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Expressions/RequestExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class RequestExpression : RuntimeExpression
/// <param name="source">The source of the request.</param>
public RequestExpression(SourceExpression source)
{
Source = source ?? throw Error.ArgumentNull(nameof(source));
Source = Utils.CheckArgumentNull(source);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Expressions/ResponseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class ResponseExpression : RuntimeExpression
/// <param name="source">The source of the response.</param>
public ResponseExpression(SourceExpression source)
{
Source = source ?? throw Error.ArgumentNull(nameof(source));
Source = Utils.CheckArgumentNull(source);
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public abstract class RuntimeExpression : IEquatable<RuntimeExpression>
/// <returns>The built runtime expression object.</returns>
public static RuntimeExpression Build(string expression)
{
if (string.IsNullOrWhiteSpace(expression))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(expression));
}
Utils.CheckArgumentNullOrEmpty(expression);

if (!expression.StartsWith(Prefix))
{
Expand Down
13 changes: 3 additions & 10 deletions src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@ public static class OpenApiExtensibleExtensions
public static void AddExtension<T>(this T element, string name, IOpenApiExtension any)
where T : IOpenApiExtensible
{
if (element == null)
{
throw Error.ArgumentNull(nameof(element));
}

if (string.IsNullOrWhiteSpace(name))
{
throw Error.ArgumentNullOrWhiteSpace(nameof(name));
}
Utils.CheckArgumentNull(element);
Utils.CheckArgumentNullOrEmpty(name);

if (!name.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix))
{
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
}

element.Extensions[name] = any ?? throw Error.ArgumentNull(nameof(any));
element.Extensions[name] = Utils.CheckArgumentNull(any);
}
}
}
21 changes: 4 additions & 17 deletions src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public static void Serialize<T>(
OpenApiWriterSettings settings)
where T : IOpenApiSerializable
{
if (stream == null)
{
throw Error.ArgumentNull(nameof(stream));
}
Utils.CheckArgumentNull(stream);

var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture);

Expand All @@ -105,15 +102,8 @@ public static void Serialize<T>(
public static void Serialize<T>(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion)
where T : IOpenApiSerializable
{
if (element == null)
{
throw Error.ArgumentNull(nameof(element));
}

if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(element);
Utils.CheckArgumentNull(writer);

switch (specVersion)
{
Expand Down Expand Up @@ -173,10 +163,7 @@ public static string Serialize<T>(
OpenApiFormat format)
where T : IOpenApiSerializable
{
if (element == null)
{
throw Error.ArgumentNull(nameof(element));
}
Utils.CheckArgumentNull(element);

using var stream = new MemoryStream();
element.Serialize(stream, specVersion, format);
Expand Down
16 changes: 3 additions & 13 deletions src/Microsoft.OpenApi/Models/OpenApiCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,8 @@ public OpenApiCallback(OpenApiCallback callback)
/// <param name="pathItem">The path item.</param>
public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem)
{
if (expression == null)
{
throw Error.ArgumentNull(nameof(expression));
}

if (pathItem == null)
{
throw Error.ArgumentNull(nameof(pathItem));
}
Utils.CheckArgumentNull(expression);
Utils.CheckArgumentNull(pathItem);

if (PathItems == null)
{
Expand All @@ -80,10 +73,7 @@ public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

var target = this;

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Models/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ public OpenApiComponents(OpenApiComponents components)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

// If references have been inlined we don't need the to render the components section
// however if they have cycles, then we will need a component rendered
Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Models/OpenApiContact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ public void SerializeAsV2(IOpenApiWriter writer)

private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down
10 changes: 2 additions & 8 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public OpenApiDocument(OpenApiDocument document)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down Expand Up @@ -141,10 +138,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Models/OpenApiEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public OpenApiEncoding(OpenApiEncoding encoding)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull("writer");
}
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public OpenApiExample(OpenApiExample example)
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
Utils.CheckArgumentNull(writer);

var target = this;

Expand Down
Loading

0 comments on commit eb97519

Please sign in to comment.