/// <summary> /// Handle an IActionResult request from a controller /// </summary> /// <typeparam name="T">type of result</typeparam> /// <param name="task">async task (usually the Cosmos query)</param> /// <param name="logger">NgsaLog</param> /// <returns>IActionResult</returns> public static async Task <IActionResult> Handle <T>(Task <T> task, NgsaLog logger) { // return exception if task is null if (task == null) { logger.LogError(new EventId((int)HttpStatusCode.InternalServerError, "Exception"), nameof(Handle), "Exception: task is null", ex: new ArgumentNullException(nameof(task))); return(CreateResult(logger.ErrorMessage, HttpStatusCode.InternalServerError)); } try { // return an OK object result return(new OkObjectResult(await task.ConfigureAwait(false))); } catch (CosmosException ce) { // log and return Cosmos status code if (ce.StatusCode == HttpStatusCode.NotFound) { logger.LogWarning(new EventId((int)ce.StatusCode, string.Empty), nameof(Handle), logger.NotFoundError); return(CreateResult(logger.NotFoundError, ce.StatusCode)); } logger.LogError(new EventId((int)ce.StatusCode, "CosmosException"), nameof(Handle), $"CosmosActivityId: {ce.ActivityId}", ex: ce); return(CreateResult(logger.ErrorMessage, ce.StatusCode)); } catch (Exception ex) { // log and return exception logger.LogError(new EventId((int)HttpStatusCode.InternalServerError, "Exception"), nameof(Handle), "Exception: {ex.Message}", ex: ex); // return 500 error return(CreateResult("Internal Server Error", HttpStatusCode.InternalServerError)); } }
/// <summary> /// Builds the config for the web server /// </summary> /// <returns>Root Configuration</returns> private static IConfigurationRoot BuildConfig() { try { // standard config builder IConfigurationBuilder cfgBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false); // build the config return(cfgBuilder.Build()); } catch (Exception ex) { // log and fail Logger.LogError(nameof(BuildConfig), "Exception: {ex.Message}", ex: ex); Environment.Exit(-1); } return(null); }