Skip to content

Commit

Permalink
config error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Torkelsen committed Nov 6, 2024
1 parent 593a153 commit 99546e1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Storage.Blobs;
using HandlenettAPI.Models;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
Expand Down Expand Up @@ -32,7 +33,7 @@ public async Task<string> CopyImageToAzureBlobStorage(string oauthToken, SlackUs
{
var containerName = _config.GetValue<string>("AzureStorage:ContainerNameUserImages");
var accountName = _config.GetValue<string>("AzureStorage:AccountName");
if (string.IsNullOrEmpty(containerName)) throw new Exception("Missing config");
if (string.IsNullOrEmpty(containerName) || string.IsNullOrEmpty(accountName)) throw new ConfigurationErrorsException("Missing storage config");

var blobService = new AzureBlobStorageService(containerName, _config);
await blobService.UploadBlobAsync(slackUser.Id + ".jpg", imageStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using HandlenettAPI.DTO;
using Newtonsoft.Json.Linq;
using HandlenettAPI.Models;
using System.Configuration;

namespace HandlenettAPI.Services
{
Expand Down Expand Up @@ -50,8 +51,7 @@ public List<UserDTO> GetUsers()

private string GetAzureBlobStorageUserImageSASToken()
{
var containerName = _config.GetValue<string>("AzureStorage:ContainerNameUserImages");
if (string.IsNullOrEmpty(containerName)) throw new Exception("Missing config");
var containerName = _config.GetValue<string>("AzureStorage:ContainerNameUserImages") ?? throw new ConfigurationErrorsException("Missing storage config");
var azureService = new AzureBlobStorageService(containerName, _config);
return azureService.GenerateContainerSasToken();
}
Expand Down Expand Up @@ -94,8 +94,7 @@ public async Task AddUserIfNotExists(GraphServiceClient graphServiceClient, Slac
{
throw new Exception("Could not get Ad profile");
}
var slackToken = _config.GetValue<string>("SlackBotUserOAuthToken");
if (string.IsNullOrEmpty(slackToken)) throw new Exception("Missing config");
var slackToken = _config.GetValue<string>("SlackBotUserOAuthToken") ?? throw new Exception("Missing config");

var SQLUser = GetUserWithDetails(new Guid(ADUser.Id));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HandlenettAPI.Models;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using System.Configuration;
using System.Text.Json;

namespace HandlenettAPI.Services
Expand All @@ -15,7 +16,9 @@ public WeatherService(HttpClient httpClient, IConfiguration config)
{
_httpClient = httpClient;
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Miles Haugesund Handlenett/1.0 ([email protected])");
_redis = ConnectionMultiplexer.Connect(config.GetConnectionString("AzureRedisCache"));
var redisConnString = config.GetConnectionString("AzureRedisCache") ?? throw new ConfigurationErrorsException("Missing redis config");
_redis = ConnectionMultiplexer.Connect(redisConnString);

}

public async Task<Weather> GetWeatherByCoordinatesAsync(double latitude, double longitude)
Expand Down

0 comments on commit 99546e1

Please sign in to comment.