Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Core/Exceptions/PluginNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace CPlugin.Net;

/// <summary>
/// The exception that is thrown when a plugin assembly cannot be found.
/// </summary>
public class PluginNotFoundException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PluginNotFoundException"/> class.
/// </summary>
/// <param name="message">
/// The message that describes the error.
/// </param>
public PluginNotFoundException(string message) : base(message)
{
}
}
14 changes: 14 additions & 0 deletions src/Core/PluginLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ internal class PluginLoadContext : AssemblyLoadContext

public PluginLoadContext(string pluginPath)
{
if (!File.Exists(pluginPath))
{
var message =
$"""
The plugin '{Path.GetFileName(pluginPath)}' was not found at:

{pluginPath}

Ensure the plugin project has been built and the assembly exists in the plugins directory.
""";

throw new PluginNotFoundException(message);
}

_resolver = new AssemblyDependencyResolver(pluginPath);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Core/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static class PluginLoader
/// <exception cref="PluginDependencyException">
/// A plugin declares a dependency that cannot be resolved.
/// </exception>
/// <exception cref="PluginNotFoundException">
/// A configured plugin assembly could not be found.
/// </exception>
public static void Load(CPluginConfigurationBase configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
Expand Down
17 changes: 17 additions & 0 deletions tests/CPlugin.Net/Core/PluginLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,21 @@ public void Load_WhenPluginDependencyIsResolved_ShouldLoadPluginsSuccessfully()
string weapons = gameModes[0].ExecuteAction();
weapons.Should().Be("Pistol, AK-47");
}

[Test]
public void Load_WhenPluginIsNotFound_ShouldThrowPluginNotFoundException()
{
// Arrange
var value = "Example.EconomyPlugin.dll";
Environment.SetEnvironmentVariable("PLUGINS", value);
var configuration = new CPluginEnvConfiguration();

// Act
Action act = () => PluginLoader.Load(configuration);

// Assert
act.Should()
.Throw<PluginNotFoundException>()
.WithMessage("*Example.EconomyPlugin.dll*");
}
}
Loading