Skip to content

Commit

Permalink
feat: Exposed RpcException property to SpannerException (#9527)
Browse files Browse the repository at this point in the history
feat: Exposed RpcException property to SpannerException
  • Loading branch information
anuragsrivstv committed Jan 11, 2023
1 parent 3ec2110 commit 4c96a04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
@@ -0,0 +1,42 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License").
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Grpc.Core;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Google.Cloud.Spanner.Data.Tests;
public class SpannerExceptionTests
{
[Fact]
public void SpannerExceptionContainsRpcExceptionAsInnerException()
{
var status = new Status(StatusCode.InvalidArgument, "Bad request");

var rpcExceptionMessage = "Test RpcException Message";
var rpcException = new RpcException(status, rpcExceptionMessage);

var spannerExceptionWithInnerExcpetion1 = new SpannerException(rpcException);
var spannerExceptionWithInnerExcpetion2 = new SpannerException(ErrorCode.InvalidArgument, rpcException);

var spannerExceptionWithoutInnerException = new SpannerException(ErrorCode.InvalidArgument, "Invalid Argument Message");

Assert.Same(rpcException, spannerExceptionWithInnerExcpetion1.RpcException);
Assert.Same(rpcException, spannerExceptionWithInnerExcpetion2.RpcException);

Assert.Null(spannerExceptionWithoutInnerException.RpcException);
}
}
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,6 +93,12 @@ public bool IsRetryable
/// </summary>
public TimeSpan? RecommendedRetryDelay { get; }

/// <summary>
/// Returns the underlying <see cref="Grpc.Core.RpcException"/>. The returned value
/// may be null if the <see cref="SpannerException"/> was not caused by an RpcException.
/// </summary>
public RpcException RpcException => InnerException as RpcException;

// For retrying aborted commits, we need to diferentiate whether the operation
// was aborted due to a session expiry. If the session expired we cannot
// retry automatically because we cannot reuse the session.
Expand Down

0 comments on commit 4c96a04

Please sign in to comment.