<commit_before>﻿using System.Diagnostics;

namespace DynamicPowerShellApi.Controllers
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Net.Http;
	using System.Threading.Tasks;
	using System.Web.Http;

	using DynamicPowerShellApi.Configuration;
	using DynamicPowerShellApi.Exceptions;

	using Newtonsoft.Json.Linq;

	/// <summary>
	/// Generic controller for running PowerShell commands.
	/// </summary>
	public class GenericController : ApiController
	{
		/// <summary>
		/// The PowerShell runner.
		/// </summary>
		private readonly IRunner _powershellRunner;

		/// <summary>
		/// Initialises a new instance of the <see cref="GenericController"/> class.
		/// </summary>
		public GenericController()
		{
		}

		/// <summary>
		/// Initialises a new instance of the <see cref="GenericController"/> class.
		/// </summary>
		/// <param name="powershellRunner">
		/// The PowerShell runner.
		/// </param>
		public GenericController(IRunner powershellRunner)
		{
			this._powershellRunner = powershellRunner;
		}

		/// <summary>
		/// The process request async.
		/// </summary>
		/// <returns>
		/// The <see cref="Task"/>.
		/// </returns>
		/// <exception cref="MalformedUriException">
		/// </exception>
		/// <exception cref="WebApiNotFoundException">
		/// </exception>
		/// <exception cref="WebMethodNotFoundException">
		/// </exception>
		/// <exception cref="MissingParametersException">
		/// </exception>
		/// <exception cref="Exception">
		/// </exception>
		[Authorize]
		public async Task<HttpResponseMessage> ProcessRequestAsync()
		{
			DynamicPowershellApiEvents
				.Raise
				.ReceivedRequest(Request.RequestUri.ToString());

			if (Request.RequestUri.Segments.Length < 4)
				throw new MalformedUriException(string.Format("There is {0} segments but must be at least 4 segments in the URI.", Request.RequestUri.Segments.Length));

			string apiName = Request.RequestUri.Segments[2].Replace("/", string.Empty);
			string methodName = Request.RequestUri.Segments[3].Replace("/", string.Empty);

			// find the api.
			var api = WebApiConfiguration.Instance.Apis[apiName];
			if (api == null)
				throw new WebApiNotFoundException(string.Format("Cannot find the requested web API: {0}", apiName));

			// find the web method.
			WebMethod method = api.WebMethods[methodName];
			if (method == null)
				throw new WebMethodNotFoundException(string.Format("Cannot find web method: {0}", methodName));

			// Get our parameters.
			IEnumerable<KeyValuePair<string, string>> query = Request.GetQueryNameValuePairs();
			if (method.Parameters.Any(param => query.All(q => q.Key != param.Name)))
				throw new MissingParametersException("Cannot find all parameters required.");

			try
			{
				var output = await _powershellRunner.ExecuteAsync(method.PowerShellPath, method.Snapin, query.ToList());	
			
				var token = output.StartsWith("[") ? (JToken)JArray.Parse(output) : JObject.Parse(output);

				return new HttpResponseMessage { Content = new JsonContent(token) };
			}
			catch (Exception exception)
			{
				DynamicPowershellApiEvents
					.Raise
					.ScriptExecutionException(exception.Message);

				Debug.WriteLine(exception.Message);
				Debug.WriteLine(exception.StackTrace);

				throw new Exception("Error with the PowerShell script output.");
			}
		} 
	}
}
<commit_msg>Added more information to exception<commit_after>﻿using System.Diagnostics;

namespace DynamicPowerShellApi.Controllers
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Net.Http;
	using System.Threading.Tasks;
	using System.Web.Http;

	using DynamicPowerShellApi.Configuration;
	using DynamicPowerShellApi.Exceptions;

	using Newtonsoft.Json.Linq;

	/// <summary>
	/// Generic controller for running PowerShell commands.
	/// </summary>
	public class GenericController : ApiController
	{
		/// <summary>
		/// The PowerShell runner.
		/// </summary>
		private readonly IRunner _powershellRunner;

		/// <summary>
		/// Initialises a new instance of the <see cref="GenericController"/> class.
		/// </summary>
		public GenericController()
		{
		}

		/// <summary>
		/// Initialises a new instance of the <see cref="GenericController"/> class.
		/// </summary>
		/// <param name="powershellRunner">
		/// The PowerShell runner.
		/// </param>
		public GenericController(IRunner powershellRunner)
		{
			this._powershellRunner = powershellRunner;
		}

		/// <summary>
		/// The process request async.
		/// </summary>
		/// <returns>
		/// The <see cref="Task"/>.
		/// </returns>
		/// <exception cref="MalformedUriException">
		/// </exception>
		/// <exception cref="WebApiNotFoundException">
		/// </exception>
		/// <exception cref="WebMethodNotFoundException">
		/// </exception>
		/// <exception cref="MissingParametersException">
		/// </exception>
		/// <exception cref="Exception">
		/// </exception>
		[Authorize]
		public async Task<HttpResponseMessage> ProcessRequestAsync()
		{
			DynamicPowershellApiEvents
				.Raise
				.ReceivedRequest(Request.RequestUri.ToString());

			if (Request.RequestUri.Segments.Length < 4)
				throw new MalformedUriException(string.Format("There is {0} segments but must be at least 4 segments in the URI.", Request.RequestUri.Segments.Length));

			string apiName = Request.RequestUri.Segments[2].Replace("/", string.Empty);
			string methodName = Request.RequestUri.Segments[3].Replace("/", string.Empty);

			// find the api.
			var api = WebApiConfiguration.Instance.Apis[apiName];
			if (api == null)
				throw new WebApiNotFoundException(string.Format("Cannot find the requested web API: {0}", apiName));

			// find the web method.
			WebMethod method = api.WebMethods[methodName];
			if (method == null)
				throw new WebMethodNotFoundException(string.Format("Cannot find web method: {0}", methodName));

			// Get our parameters.
			IEnumerable<KeyValuePair<string, string>> query = Request.GetQueryNameValuePairs();
			if (method.Parameters.Any(param => query.All(q => q.Key != param.Name)))
				throw new MissingParametersException("Cannot find all parameters required.");

			try
			{
				var output = await _powershellRunner.ExecuteAsync(method.PowerShellPath, method.Snapin, query.ToList());	
			
				var token = output.StartsWith("[") ? (JToken)JArray.Parse(output) : JObject.Parse(output);

				return new HttpResponseMessage { Content = new JsonContent(token) };
			}
			catch (Exception exception)
			{
				DynamicPowershellApiEvents
					.Raise
					.ScriptExecutionException(exception.Message);

				Debug.WriteLine(exception.Message);
				Debug.WriteLine(exception.StackTrace);

				throw new Exception(
					String.Format(
						"Error running PowerShell script. Check '{0}' Event Source for more information.", 
						DynamicPowershellApiEvents.EventSourceName
					));
			}
		} 
	}
}

{'fraction_non_alphanumeric': 0.13275862068965516, 'fraction_numerical': 0.0021551724137931034, 'mean_word_length': 3.2969135802469136, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6937237', 'n_tokens_mistral': 2274, 'n_tokens_neox': 1938, 'n_words': 472}