Skip to content

Commit

Permalink
Updates batch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrueastman committed Mar 30, 2023
1 parent 366662e commit 661d962
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ If you are looking to build the library locally for the purposes of contributing
- Run `dotnet restore` from the command line in your package directory
- Run `nuget restore` and `msbuild` from CLI or run Build from Visual Studio to restore Nuget packages and build the project

> Due to long file names you may need to run `git config --system core.longpaths true` before cloning the repo to your system.
## License

Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT [license](LICENSE.txt). See [Third Party Notices](https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/master/THIRD%20PARTY%20NOTICES) for information on the packages referenced via NuGet.
60 changes: 60 additions & 0 deletions src/Microsoft.Graph/Extensions/CustomBatchRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph.Models.ODataErrors;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Serialization;

namespace Microsoft.Graph.Requests;

public class CustomBatchRequestBuilder: Core.Requests.BatchRequestBuilder
{
/// <summary>
/// Constructs a new BatchRequestBuilder.
/// </summary>
/// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
public CustomBatchRequestBuilder(IRequestAdapter requestAdapter): base(requestAdapter)
{
_ = requestAdapter ?? throw new ArgumentNullException(nameof(requestAdapter));
}

/// <summary>
/// Sends out the <see cref="BatchRequestContent"/> using the POST method
/// </summary>
/// <param name="batchRequestContent">The <see cref="BatchRequestContent"/> for the request</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for cancelling requests</param>
/// <param name="errorMappings">The error mappings for using to handle errors in batch request</param>
/// <returns></returns>
public new Task<BatchResponseContent> PostAsync(BatchRequestContent batchRequestContent, CancellationToken cancellationToken = default, Dictionary<string, ParsableFactory<IParsable>> errorMappings = null)
{
var batchErrorMappings = errorMappings ?? new Dictionary<string, ParsableFactory<IParsable>> {
{"4XX", ODataError.CreateFromDiscriminatorValue},
{"5XX", ODataError.CreateFromDiscriminatorValue},
};

return base.PostAsync(batchRequestContent, cancellationToken, batchErrorMappings);
}

/// <summary>
/// Sends out the <see cref="BatchRequestContentCollection"/> using the POST method
/// </summary>
/// <param name="batchRequestContentCollection">The <see cref="BatchRequestContentCollection"/> for the request</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for cancelling requests</param>
/// <param name="errorMappings">The error mappings for using to handle errors in batch request</param>
/// <returns></returns>
public new Task<BatchResponseContentCollection> PostAsync(BatchRequestContentCollection batchRequestContentCollection, CancellationToken cancellationToken = default, Dictionary<string, ParsableFactory<IParsable>> errorMappings = null)
{
var batchErrorMappings = errorMappings ?? new Dictionary<string, ParsableFactory<IParsable>> {
{"4XX", ODataError.CreateFromDiscriminatorValue},
{"5XX", ODataError.CreateFromDiscriminatorValue},
};

return base.PostAsync(batchRequestContentCollection, cancellationToken, batchErrorMappings);
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.Graph/GraphServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Graph
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Graph.Requests;
using Microsoft.Graph.Core.Requests;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Authentication.Azure;
Expand Down Expand Up @@ -89,7 +90,7 @@ public BatchRequestBuilder Batch
{
get
{
return new BatchRequestBuilder(this.RequestAdapter);
return new CustomBatchRequestBuilder(this.RequestAdapter);
}
}

Expand Down

0 comments on commit 661d962

Please sign in to comment.