mirror of https://github.com/SixLabors/ImageSharp
Browse Source
* Throw ImageFormatException on load * Unseal class and make constructor internal - This is so that no one can new it up / inherit it outside of the assembly * Add new exception for distinguish between different exception - This will be used on image.load operations with invalid image streams * ImageFormatException -> UnkownImageFormatException * Add Image.Load throws exception testsaf/merge-core
committed by
James Jackson-South
4 changed files with 108 additions and 8 deletions
@ -0,0 +1,36 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// The exception that is thrown when the library tries to load
|
|||
/// an image which has an unkown format.
|
|||
/// </summary>
|
|||
public sealed class UnknownImageFormatException : ImageFormatException |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="UnknownImageFormatException"/> class with the name of the
|
|||
/// parameter that causes this exception.
|
|||
/// </summary>
|
|||
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
|
|||
public UnknownImageFormatException(string errorMessage) |
|||
: base(errorMessage) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="UnknownImageFormatException"/> class with a specified
|
|||
/// error message and the exception that is the cause of this exception.
|
|||
/// </summary>
|
|||
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
|
|||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic)
|
|||
/// if no inner exception is specified.</param>
|
|||
public UnknownImageFormatException(string errorMessage, Exception innerException) |
|||
: base(errorMessage, innerException) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
|
|||
using SixLabors.ImageSharp.Formats; |
|||
using SixLabors.ImageSharp.Formats.Bmp; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
using Xunit; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests |
|||
{ |
|||
public partial class ImageTests |
|||
{ |
|||
public class Load_FromStream_Throws : IDisposable |
|||
{ |
|||
private static readonly byte[] Data = new byte[] { 0x01 }; |
|||
|
|||
private MemoryStream Stream { get; } = new MemoryStream(Data); |
|||
|
|||
[Fact] |
|||
public void Image_Load_Throws_UknownImageFormatException() |
|||
{ |
|||
Assert.Throws<UnknownImageFormatException>(() => |
|||
{ |
|||
using (var img = Image.Load(Configuration.Default, this.Stream, out IImageFormat format)) |
|||
{ |
|||
} |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Image_Load_T_Throws_UknownImageFormatException() |
|||
{ |
|||
Assert.Throws<UnknownImageFormatException>(() => |
|||
{ |
|||
using (var img = Image.Load<Rgba32>(Configuration.Default, this.Stream, out IImageFormat format)) |
|||
{ |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
this.Stream?.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue