Skip to content

Commit e5cc8de

Browse files
authored
Changed Error Message if Entity is not configured in dab-config json (#2186)
## Why make this change? As mentioned here #887, if entity is not configured in DAB Config JSON file, it throws a generic exception i.e. keyNotFoundException. As part of this PR, changed this message to make it clearer. ## What is this change? If Entity is not configured then DAB will throw `System.ApplicationException` with error message as _"The entity '<EntityName>' was not found in the dab-config json"_ ## How was this tested? - [ ] Integration Tests - [ ] Unit Tests ## Sample Request(s) - Example REST and/or GraphQL request to demonstrate modifications - Example of CLI usage to demonstrate modifications
1 parent 54308cc commit e5cc8de

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/Config/ObjectModel/RuntimeEntities.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,21 @@ public bool ContainsKey(string key)
5656
return Entities.ContainsKey(key);
5757
}
5858

59-
public Entity this[string key] => Entities[key];
59+
public Entity this[string key]
60+
{
61+
get
62+
{
63+
Entity? entity;
64+
if (Entities.TryGetValue(key, out entity) && entity is not null)
65+
{
66+
return entity;
67+
}
68+
else
69+
{
70+
throw new ApplicationException($"The entity '{key}' was not found in the dab-config json");
71+
}
72+
}
73+
}
6074

6175
IEnumerator IEnumerable.GetEnumerator()
6276
{

src/Service.Tests/Configuration/ConfigurationTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Threading.Tasks;
2020
using System.Web;
2121
using Azure.DataApiBuilder.Config;
22+
using Azure.DataApiBuilder.Config.NamingPolicies;
2223
using Azure.DataApiBuilder.Config.ObjectModel;
2324
using Azure.DataApiBuilder.Core.AuthenticationHelpers;
2425
using Azure.DataApiBuilder.Core.Authorization;
@@ -2937,6 +2938,63 @@ public async Task TestEngineSupportViewsWithoutKeyFieldsInConfigForMsSQL()
29372938
}
29382939
}
29392940

2941+
[TestMethod, TestCategory(TestCategory.COSMOSDBNOSQL)]
2942+
public async Task TestErrorMessageWithoutKeyFieldsInConfig()
2943+
{
2944+
Dictionary<string, object> dbOptions = new();
2945+
HyphenatedNamingPolicy namingPolicy = new();
2946+
2947+
dbOptions.Add(namingPolicy.ConvertName(nameof(CosmosDbNoSQLDataSourceOptions.Database)), "graphqldb");
2948+
dbOptions.Add(namingPolicy.ConvertName(nameof(CosmosDbNoSQLDataSourceOptions.Container)), "dummy");
2949+
dbOptions.Add(namingPolicy.ConvertName(nameof(CosmosDbNoSQLDataSourceOptions.Schema)), "custom-schema.gql");
2950+
DataSource dataSource = new(DatabaseType.CosmosDB_NoSQL,
2951+
GetConnectionStringFromEnvironmentConfig(environment: TestCategory.COSMOSDBNOSQL), Options: dbOptions);
2952+
Entity entity = new(
2953+
Source: new("EntityName", EntitySourceType.Table, null, null),
2954+
Rest: new(Enabled: false),
2955+
GraphQL: new("", ""),
2956+
Permissions: new[] { GetMinimalPermissionConfig(AuthorizationResolver.ROLE_ANONYMOUS) },
2957+
Relationships: null,
2958+
Mappings: null
2959+
);
2960+
2961+
RuntimeConfig configuration = InitMinimalRuntimeConfig(dataSource, new(), new(), entity, "EntityName");
2962+
2963+
const string CUSTOM_CONFIG = "custom-config.json";
2964+
2965+
File.WriteAllText(
2966+
CUSTOM_CONFIG,
2967+
configuration.ToJson());
2968+
2969+
string[] args = new[]
2970+
{
2971+
$"--ConfigFileName={CUSTOM_CONFIG}"
2972+
};
2973+
2974+
using (TestServer server = new(Program.CreateWebHostBuilder(args)))
2975+
using (HttpClient client = server.CreateClient())
2976+
{
2977+
string query = @"{
2978+
EntityName {
2979+
items{
2980+
id
2981+
title
2982+
}
2983+
}
2984+
}";
2985+
2986+
object payload = new { query };
2987+
2988+
HttpRequestMessage graphQLRequest = new(HttpMethod.Post, "/graphql")
2989+
{
2990+
Content = JsonContent.Create(payload)
2991+
};
2992+
2993+
ApplicationException ex = await Assert.ThrowsExceptionAsync<ApplicationException>(async () => await client.SendAsync(graphQLRequest));
2994+
Assert.AreEqual(ex.Message, "The entity 'Planet' was not found in the dab-config json");
2995+
}
2996+
}
2997+
29402998
/// <summary>
29412999
/// Tests that Startup.cs properly handles EasyAuth authentication configuration.
29423000
/// AppService as Identity Provider while in Production mode will result in startup error.

0 commit comments

Comments
 (0)