diff --git a/DevProxy/Commands/DevProxyCommand.cs b/DevProxy/Commands/DevProxyCommand.cs index b581b54a..fce40118 100644 --- a/DevProxy/Commands/DevProxyCommand.cs +++ b/DevProxy/Commands/DevProxyCommand.cs @@ -14,6 +14,7 @@ sealed class DevProxyCommand : RootCommand private readonly IProxyConfiguration _proxyConfiguration; private readonly ISet _urlsToWatch; private readonly UpdateNotification _updateNotification; + private readonly IConfiguration _configuration; private WebApplication? _app; internal const string PortOptionName = "--port"; @@ -59,6 +60,7 @@ public DevProxyCommand( IProxyConfiguration proxyConfiguration, IServiceProvider serviceProvider, UpdateNotification updateNotification, + IConfiguration configuration, ILogger logger) : base("Start Dev Proxy") { _serviceProvider = serviceProvider; @@ -66,6 +68,7 @@ public DevProxyCommand( _urlsToWatch = urlsToWatch; _proxyConfiguration = proxyConfiguration; _updateNotification = updateNotification; + _configuration = configuration; _logger = logger; ConfigureCommand(); @@ -84,6 +87,11 @@ private async Task InvokeAsync(ParseResult parseResult, CancellationToken c { throw new InvalidOperationException("WebApplication instance is not set. Please provide it when invoking the command."); } + if ((_configuration as IConfigurationRoot)?.Providers.Any(p => p is FileConfigurationProvider) != true) + { + _logger.LogError("No configuration file found. Please create a devproxyrc.json file in the current directory."); + return 1; + } if (!_plugins.Any()) { _logger.LogError("You haven't configured any plugins. Please add plugins to your configuration file. Dev Proxy will exit."); diff --git a/DevProxy/Extensions/ConfigurationManagerExtensions.cs b/DevProxy/Extensions/ConfigurationManagerExtensions.cs index d9b7536b..1cf89a1f 100644 --- a/DevProxy/Extensions/ConfigurationManagerExtensions.cs +++ b/DevProxy/Extensions/ConfigurationManagerExtensions.cs @@ -38,6 +38,6 @@ public static ConfigurationManager ConfigureDevProxyConfig(this ConfigurationMan } } - throw new InvalidOperationException("No configuration file found. Please create a devproxyrc.json file in the current directory."); + return configuration; } } \ No newline at end of file diff --git a/DevProxy/Plugins/PluginServiceExtensions.cs b/DevProxy/Plugins/PluginServiceExtensions.cs index e3241da1..eb9943ca 100644 --- a/DevProxy/Plugins/PluginServiceExtensions.cs +++ b/DevProxy/Plugins/PluginServiceExtensions.cs @@ -68,7 +68,8 @@ public static IServiceCollection AddPlugins( if (pluginReferences is null || !pluginReferences.Any(p => p.Enabled)) { - throw new InvalidOperationException("No plugins configured or enabled. Please add a plugin to the configuration file."); + _ = services.AddSingleton>(globallyWatchedUrls.ToHashSet()); + return services; } var defaultUrlsToWatch = globallyWatchedUrls.ToHashSet(); diff --git a/DevProxy/devproxy-errors.json b/DevProxy/devproxy-errors.json deleted file mode 100644 index bf3809fd..00000000 --- a/DevProxy/devproxy-errors.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/genericrandomerrorplugin.errorsfile.schema.json", - "errors": [ - { - "request": { - "url": "https://*/*" - }, - "responses": [ - { - "statusCode": 400, - "body": { - "message": "Bad Request", - "details": "The server cannot process the request due to invalid syntax." - } - }, - { - "statusCode": 401, - "body": { - "message": "Unauthorized", - "details": "The request requires user authentication." - } - }, - { - "statusCode": 403, - "body": { - "message": "Forbidden", - "details": "The server understood the request, but refuses to authorize it." - } - }, - { - "statusCode": 404, - "body": { - "message": "Not Found", - "details": "The requested resource could not be found." - } - }, - { - "statusCode": 418, - "body": { - "message": "I'm a teapot", - "details": "The server refuses the attempt to brew coffee with a teapot." - } - }, - { - "statusCode": 429, - "body": { - "message": "Too Many Requests", - "details": "The user has sent too many requests in a given amount of time (\"rate limiting\")." - }, - "headers": [ - { - "name": "Retry-After", - "value": "@dynamic" - } - ] - }, - { - "statusCode": 500, - "body": { - "message": "Internal Server Error", - "details": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application." - } - }, - { - "statusCode": 503, - "body": { - "message": "Service Unavailable", - "details": "The server is currently unable to handle the request due to a temporary overload or maintenance. Please try again later." - } - } - ] - } - ] -} \ No newline at end of file diff --git a/DevProxy/devproxyrc.json b/DevProxy/devproxyrc.json deleted file mode 100644 index 08310a40..00000000 --- a/DevProxy/devproxyrc.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/rc.schema.json", - "plugins": [ - { - "name": "RetryAfterPlugin", - "enabled": true, - "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll" - }, - { - "name": "GenericRandomErrorPlugin", - "enabled": true, - "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll", - "configSection": "genericRandomErrorPlugin" - } - ], - "urlsToWatch": [ - "https://jsonplaceholder.typicode.com/*" - ], - "genericRandomErrorPlugin": { - "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/genericrandomerrorplugin.schema.json", - "errorsFile": "devproxy-errors.json", - "rate": 50 - }, - "logLevel": "information", - "newVersionNotification": "stable", - "showSkipMessages": true, - "showTimestamps": true, - "validateSchemas": true -} \ No newline at end of file