Skip to content

Commit

Permalink
refactor!: modernize exceptions (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT authored Jan 11, 2024
1 parent cca9526 commit 894ea72
Show file tree
Hide file tree
Showing 31 changed files with 3,182 additions and 3,164 deletions.
7 changes: 3 additions & 4 deletions src/OrasProject.Oras/Content/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Oci;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -91,7 +90,7 @@ internal static async Task<byte[]> ReadAllAsync(this Stream stream, Descriptor d
{
if (descriptor.Size < 0)
{
throw new InvalidDescriptorSizeException("this descriptor size is less than 0");
throw new InvalidDescriptorSizeException("Descriptor size is less than 0");
}
var buffer = new byte[descriptor.Size];
try
Expand All @@ -100,12 +99,12 @@ internal static async Task<byte[]> ReadAllAsync(this Stream stream, Descriptor d
}
catch (ArgumentOutOfRangeException)
{
throw new ArgumentOutOfRangeException("this descriptor size is less than content size");
throw new ArgumentOutOfRangeException("Descriptor size is less than content size");
}

if (Digest.ComputeSHA256(buffer) != descriptor.Digest)
{
throw new MismatchedDigestException("this descriptor digest is different from content digest");
throw new MismatchedDigestException("Descriptor digest is different from content digest");
}
return buffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@

using System;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Content;

/// <summary>
/// InvalidDescriptorSizeException is thrown when a descriptor size is invalid.
/// </summary>
public class InvalidDescriptorSizeException : ArgumentException
{
/// <summary>
/// UnsupportedException is thrown when a feature is not supported.
/// </summary>
public class UnsupportedException : Exception
public InvalidDescriptorSizeException()
{
public UnsupportedException()
{
}
}

public UnsupportedException(string message)
: base(message)
{
}
public InvalidDescriptorSizeException(string? message)
: base(message)
{
}

public UnsupportedException(string message, Exception inner)
: base(message, inner)
{
}
public InvalidDescriptorSizeException(string? message, Exception? inner)
: base(message, inner)
{
}
}
14 changes: 7 additions & 7 deletions src/OrasProject.Oras/Content/MemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ internal class MemoryStorage : IStorage
{
private readonly ConcurrentDictionary<BasicDescriptor, byte[]> _content = new();

public Task<bool> ExistsAsync(Descriptor target, CancellationToken _ = default)
{
return Task.FromResult(_content.ContainsKey(target.BasicDescriptor));
}

public Task<bool> ExistsAsync(Descriptor target, CancellationToken _ = default)
{
return Task.FromResult(_content.ContainsKey(target.BasicDescriptor));
}

public Task<Stream> FetchAsync(Descriptor target, CancellationToken _ = default)
{
if (!_content.TryGetValue(target.BasicDescriptor, out var content))
Expand All @@ -47,9 +47,9 @@ public async Task PushAsync(Descriptor expected, Stream contentStream, Cancellat
}

var content = await contentStream.ReadAllAsync(expected, cancellationToken).ConfigureAwait(false);
if (!_content.TryAdd(key, content))
if (!_content.TryAdd(key, content))
{
throw new AlreadyExistsException($"{key.Digest}: {key.MediaType}");
throw new AlreadyExistsException($"{key.Digest}: {key.MediaType}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
// limitations under the License.

using System;
using System.IO;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Content;

/// <summary>
/// MismatchedDigestException is thrown when a digest does not match the content.
/// </summary>
public class MismatchedDigestException : IOException
{
/// <summary>
/// MissingReferenceException is thrown when a reference is missing.
/// </summary>
public class MissingReferenceException : Exception
public MismatchedDigestException()
{
public MissingReferenceException()
{
}
}

public MissingReferenceException(string message)
: base(message)
{
}
public MismatchedDigestException(string? message)
: base(message)
{
}

public MissingReferenceException(string message, Exception inner)
: base(message, inner)
{
}
public MismatchedDigestException(string? message, Exception? inner)
: base(message, inner)
{
}
}
32 changes: 16 additions & 16 deletions src/OrasProject.Oras/Exceptions/AlreadyExistsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
// limitations under the License.

using System;
using System.IO;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Exceptions;

/// <summary>
/// AlreadyExistsException is thrown when a resource already exists.
/// </summary>
public class AlreadyExistsException : IOException
{
/// <summary>
/// AlreadyExistsException is thrown when a resource already exists.
/// </summary>
public class AlreadyExistsException : Exception
public AlreadyExistsException()
{
public AlreadyExistsException()
{
}
}

public AlreadyExistsException(string message)
: base(message)
{
}
public AlreadyExistsException(string? message)
: base(message)
{
}

public AlreadyExistsException(string message, Exception inner)
: base(message, inner)
{
}
public AlreadyExistsException(string? message, Exception? inner)
: base(message, inner)
{
}
}
31 changes: 15 additions & 16 deletions src/OrasProject.Oras/Exceptions/InvalidDigestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@

using System;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Exceptions;

/// <summary>
/// InvalidDigestException is thrown when a digest is invalid.
/// </summary>
public class InvalidDigestException : FormatException
{
/// <summary>
/// InvalidDigestException is thrown when a digest is invalid.
/// </summary>
public class InvalidDigestException : Exception
public InvalidDigestException()
{
public InvalidDigestException()
{
}
}

public InvalidDigestException(string message)
: base(message)
{
}
public InvalidDigestException(string? message)
: base(message)
{
}

public InvalidDigestException(string message, Exception inner)
: base(message, inner)
{
}
public InvalidDigestException(string? message, Exception? inner)
: base(message, inner)
{
}
}
31 changes: 15 additions & 16 deletions src/OrasProject.Oras/Exceptions/InvalidReferenceException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@

using System;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Exceptions;

/// <summary>
/// InvalidReferenceException is thrown when the reference is invlid
/// </summary>
public class InvalidReferenceException : FormatException
{
/// <summary>
/// InvalidReferenceException is thrown when the reference is invlid
/// </summary>
public class InvalidReferenceException : Exception
public InvalidReferenceException()
{
public InvalidReferenceException()
{
}
}

public InvalidReferenceException(string message)
: base(message)
{
}
public InvalidReferenceException(string? message)
: base(message)
{
}

public InvalidReferenceException(string message, Exception inner)
: base(message, inner)
{
}
public InvalidReferenceException(string? message, Exception? inner)
: base(message, inner)
{
}
}
37 changes: 0 additions & 37 deletions src/OrasProject.Oras/Exceptions/MismatchedDigestException.cs

This file was deleted.

32 changes: 16 additions & 16 deletions src/OrasProject.Oras/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
// limitations under the License.

using System;
using System.IO;

namespace OrasProject.Oras.Exceptions
namespace OrasProject.Oras.Exceptions;

/// <summary>
/// NotFoundException is thrown when a resource is not found.
/// </summary>
public class NotFoundException : IOException
{
/// <summary>
/// NotFoundException is thrown when a resource is not found.
/// </summary>
public class NotFoundException : Exception
public NotFoundException()
{
public NotFoundException()
{
}
}

public NotFoundException(string message)
: base(message)
{
}
public NotFoundException(string? message)
: base(message)
{
}

public NotFoundException(string message, Exception inner)
: base(message, inner)
{
}
public NotFoundException(string? message, Exception? inner)
: base(message, inner)
{
}
}
37 changes: 0 additions & 37 deletions src/OrasProject.Oras/Exceptions/SizeExceedsLimitException.cs

This file was deleted.

Loading

0 comments on commit 894ea72

Please sign in to comment.