Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KenitoInc committed May 29, 2023
1 parent 30c8729 commit 7c8b584
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
24 changes: 19 additions & 5 deletions src/Microsoft.OData.Client/DeepInsertSaveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.OData.Edm;

namespace Microsoft.OData.Client
{
Expand Down Expand Up @@ -124,7 +126,8 @@ internal void BuildDescriptorGraph<T>(IEnumerable<Descriptor> descriptors, bool
// We don't support delete and update in deep insert.
if (entityDescriptor.State == EntityStates.Deleted || entityDescriptor.State == EntityStates.Modified)
{
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified);
string entitySetAndKey = GetEntitySetAndKey(entityDescriptor);
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified(entitySetAndKey));
}

bulkUpdateGraph.AddRelatedDescriptor(topLevelDescriptor, entityDescriptor);
Expand All @@ -135,17 +138,19 @@ internal void BuildDescriptorGraph<T>(IEnumerable<Descriptor> descriptors, bool
{
if (linkDescriptor.Source.Equals(topLevelDescriptor.Entity) && !bulkUpdateGraph.Contains(linkDescriptor))
{
EntityDescriptor targetDescriptor = this.RequestInfo.Context.GetEntityDescriptor(linkDescriptor.Target);

// We don't support delete and update in deep insert.
if (linkDescriptor.State == EntityStates.Deleted || linkDescriptor.State == EntityStates.Modified)
{
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified);
string entitySetAndKey = GetEntitySetAndKey(targetDescriptor);
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified(entitySetAndKey));
}

EntityDescriptor targetDescriptor = this.RequestInfo.Context.GetEntityDescriptor(linkDescriptor.Target);

if (targetDescriptor != null && (targetDescriptor.State == EntityStates.Deleted || targetDescriptor.State == EntityStates.Modified))
{
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified);
string entitySetAndKey = GetEntitySetAndKey(targetDescriptor);
throw Error.InvalidOperation(Strings.Context_DeepInsertDeletedOrModified(entitySetAndKey));
}

bulkUpdateGraph.AddRelatedDescriptor(topLevelDescriptor, linkDescriptor);
Expand All @@ -156,6 +161,15 @@ internal void BuildDescriptorGraph<T>(IEnumerable<Descriptor> descriptors, bool
}
}

private string GetEntitySetAndKey(EntityDescriptor entityDescriptor)
{
var baseUriString = this.RequestInfo.BaseUriResolver.GetBaseUriWithSlash().ToString();
IEdmModel model = this.RequestInfo.Format.ServiceModel;
string entitySetUri = entityDescriptor.EditLink.ToString().TrimStart(baseUriString.ToCharArray());

return entitySetUri;
}

/// <summary>
/// Creates an ODataRequestMessage for a deep insert request.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Context_MustBeUsedWith='{0}' must be used with '{1}'.
Context_EntityMediaLinksNotTrackedInEntity=The context is currently in no tracking mode, in order to use streams make sure your entities extend BaseEntityType and query the Item again from the server to populate the read link or enable tracking.
Context_EntityInNonTrackedContextLacksMediaLinks=The context is in non tracking mode, The entity does not seem to have the media links populated properly. Please verify server response is correct and that the entity extends BaseEntityType.
Context_DeepInsertOneTopLevelEntity=Deep insert can only have one top level entity.
Context_DeepInsertDeletedOrModified=For deep insert ChangeState cannot be Deleted or Modified.
Context_DeepInsertDeletedOrModified=For deep insert, ChangeState for '{0}' cannot be Deleted or Modified.

DataServiceClientFormat_LoadServiceModelRequired=When you call the UseJson method without a parameter, you must use the LoadServiceModel property to provide a valid IEdmModel instance.
DataServiceClientFormat_ValidServiceModelRequiredForJson=To use the JSON format, you must first call DataServiceContext.Format.UseJson() and supply a valid service model.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Client/Microsoft.OData.Client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Context_OnMessageCreatingReturningNull=DataServiceContext.Configurations.Request
Context_SendingRequest_InvalidWhenUsingOnMessageCreating=SendingRequest cannot be used in combination with the DataServiceContext.Configurations.RequestPipeline.OnMessageCreating property. Please use SendingRequest2 with DataServiceContext.Configurations.RequestPipeline.OnMessageCreating property instead.
Context_MustBeUsedWith='{0}' must be used with '{1}'.
Context_DeepInsertOneTopLevelEntity=Deep insert can only have one top level entity.
Context_DeepInsertDeletedOrModified=For deep insert ChangeState cannot be Deleted or Modified.
Context_DeepInsertDeletedOrModified=For deep insert, ChangeState for '{0}' cannot be Deleted or Modified.

DataServiceClientFormat_LoadServiceModelRequired=When you call the UseJson method without a parameter, you must use the LoadServiceModel property to provide a valid IEdmModel instance.
DataServiceClientFormat_ValidServiceModelRequiredForJson=To use the JSON format, you must first call DataServiceContext.Format.UseJson() and supply a valid service model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,11 @@ internal static string Context_DeepInsertOneTopLevelEntity
}

/// <summary>
/// A string like "For deep insert ChangeState cannot be Deleted or Modified."
/// A string like "For deep insert, ChangeState for '{0}' cannot be Deleted or Modified."
/// </summary>
internal static string Context_DeepInsertDeletedOrModified
internal static string Context_DeepInsertDeletedOrModified(object p0)
{
get
{
return Microsoft.OData.Client.TextRes.GetString(Microsoft.OData.Client.TextRes.Context_DeepInsertDeletedOrModified);
}
return Microsoft.OData.Client.TextRes.GetString(Microsoft.OData.Client.TextRes.Context_DeepInsertDeletedOrModified, p0);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void SerializeEntry_With_DeleteLink_Fails()
this.GetBulkUpdateGraph(person);
};

action.ShouldThrow<InvalidOperationException>(Strings.Context_DeepInsertDeletedOrModified);
action.ShouldThrow<InvalidOperationException>(Strings.Context_DeepInsertDeletedOrModified("Cars(1002)"));
}

[Fact]
Expand Down Expand Up @@ -301,7 +301,7 @@ public void SerializeEntry_With_UpdateObject_Fails()
this.GetBulkUpdateGraph(person);
};

action.ShouldThrow<InvalidOperationException>(Strings.Context_DeepInsertDeletedOrModified);
action.ShouldThrow<InvalidOperationException>(Strings.Context_DeepInsertDeletedOrModified("Cars(200)"));
}

[Fact]
Expand Down

0 comments on commit 7c8b584

Please sign in to comment.