Skip to content

Commit

Permalink
feat: update SdkException and subtypes
Browse files Browse the repository at this point in the history
Adds errorCode and transportDetails properties.
  • Loading branch information
pgautier404 committed Sep 20, 2022
1 parent ddfa8e1 commit 16af95d
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 28 deletions.
3 changes: 2 additions & 1 deletion examples/MomentoApplication/MomentoApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Momento.Sdk" Version="0.26.0" />
<!-- <PackageReference Include="Momento.Sdk" Version="0.26.0" /> -->
<ProjectReference Include="../../src/Momento.Sdk/Momento.Sdk.csproj" />
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/AlreadyExistsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class AlreadyExistsException : SdkException
{
public AlreadyExistsException(string message) : base(message)
public AlreadyExistsException(string message) : base(message, MomentoErrorCode.ALREADY_EXISTS_ERROR)
{
}
public AlreadyExistsException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.ALREADY_EXISTS_ERROR, transportDetails)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/AuthenticationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class AuthenticationException : SdkException
{
public AuthenticationException(string message) : base(message)
public AuthenticationException(string message) : base(message, MomentoErrorCode.AUTHENTICATION_ERROR)
{
}
public AuthenticationException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.AUTHENTICATION_ERROR, transportDetails)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class BadRequestException : SdkException
{
public BadRequestException(string message) : base(message)
public BadRequestException(string message) : base(message, MomentoErrorCode.BAD_REQUEST_ERROR)
{
}
public BadRequestException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.BAD_REQUEST_ERROR, transportDetails)
{
}
}
24 changes: 14 additions & 10 deletions src/Momento.Sdk/Exceptions/CacheExceptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,46 @@ public static Exception Convert(Exception e)
}
if (e is RpcException ex)
{
MomentoErrorTransportDetails transportDetails = new MomentoErrorTransportDetails(
new MomentoGrpcErrorDetails(ex.StatusCode, ex.Message, null)
);

switch (ex.StatusCode)
{
case StatusCode.InvalidArgument:
case StatusCode.OutOfRange:
case StatusCode.Unimplemented:
return new BadRequestException(ex.Message);
return new BadRequestException(ex.Message, transportDetails);

case StatusCode.FailedPrecondition:
return new FailedPreconditionException(ex.Message);
return new FailedPreconditionException(ex.Message, transportDetails);

case StatusCode.PermissionDenied:
return new PermissionDeniedException(ex.Message);
return new PermissionDeniedException(ex.Message, transportDetails);

case StatusCode.Unauthenticated:
return new AuthenticationException(ex.Message);
return new AuthenticationException(ex.Message, transportDetails);

case StatusCode.ResourceExhausted:
return new LimitExceededException(ex.Message);
return new LimitExceededException(ex.Message, transportDetails);

case StatusCode.NotFound:
return new NotFoundException(ex.Message);
return new NotFoundException(ex.Message, transportDetails);

case StatusCode.AlreadyExists:
return new AlreadyExistsException(ex.Message);
return new AlreadyExistsException(ex.Message, transportDetails);

case StatusCode.DeadlineExceeded:
return new TimeoutException(ex.Message);
return new TimeoutException(ex.Message, transportDetails);

case StatusCode.Cancelled:
return new CancelledException(ex.Message);
return new CancelledException(ex.Message, transportDetails);

case StatusCode.Unknown:
case StatusCode.Unavailable:
case StatusCode.Aborted:
case StatusCode.DataLoss:
default: return new InternalServerException(INTERNAL_SERVER_ERROR_MESSAGE, e);
default: return new InternalServerException(INTERNAL_SERVER_ERROR_MESSAGE, transportDetails, e);
}
}
return new UnknownException(SDK_ERROR_MESSAGE, e);
Expand Down
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/CancelledException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ public class CancelledException : SdkException
/// <summary>
/// Operation was cancelled.
/// </summary>
public CancelledException(string message) : base(message)
public CancelledException(string message) : base(message, MomentoErrorCode.CANCELLED_ERROR)
{
}
public CancelledException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.CANCELLED_ERROR, transportDetails)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/FailedPreconditionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace Momento.Sdk.Exceptions;
/// </summary>
public class FailedPreconditionException : SdkException
{
public FailedPreconditionException(string message) : base(message)
public FailedPreconditionException(string message) : base(message, MomentoErrorCode.FAILED_PRECONDITION_ERROR)
{
}
public FailedPreconditionException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.FAILED_PRECONDITION_ERROR, transportDetails)
{
}
}
7 changes: 5 additions & 2 deletions src/Momento.Sdk/Exceptions/InternalServerException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ namespace Momento.Sdk.Exceptions;
/// </summary>
public class InternalServerException : SdkException
{
public InternalServerException(string message, Exception e) : base(message, e)
public InternalServerException(string message) : base(message, MomentoErrorCode.INTERNAL_SERVER_ERROR)
{
}
public InternalServerException(string message) : base(message)
public InternalServerException(string message, Exception e) : base(message, MomentoErrorCode.INTERNAL_SERVER_ERROR, null, e)
{
}
public InternalServerException(string message, MomentoErrorTransportDetails transportDetails, Exception e) : base(message, MomentoErrorCode.INTERNAL_SERVER_ERROR, transportDetails, e)
{
}
}
4 changes: 2 additions & 2 deletions src/Momento.Sdk/Exceptions/InvalidArgumentException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Momento.Sdk.Exceptions;
/// </summary>
public class InvalidArgumentException : SdkException
{
public InvalidArgumentException(string message) : base(message)
public InvalidArgumentException(string message) : base(message, MomentoErrorCode.INVALID_ARGUMENT_ERROR)
{
}

public InvalidArgumentException(string message, Exception e) : base(message, e)
public InvalidArgumentException(string message, Exception e) : base(message, MomentoErrorCode.INVALID_ARGUMENT_ERROR, null, e)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/LimitExceededException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class LimitExceededException : SdkException
{
public LimitExceededException(string message) : base(message)
public LimitExceededException(string message) : base(message, MomentoErrorCode.LIMIT_EXCEEDED_ERROR)
{
}
public LimitExceededException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.LIMIT_EXCEEDED_ERROR, transportDetails)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class NotFoundException : SdkException
{
public NotFoundException(string message) : base(message)
public NotFoundException(string message) : base(message, MomentoErrorCode.NOT_FOUND_ERROR)
{
}
public NotFoundException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.NOT_FOUND_ERROR, transportDetails)
{
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/PermissionDeniedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class PermissionDeniedException : SdkException
{
public PermissionDeniedException(string message) : base(message)
public PermissionDeniedException(string message) : base(message, MomentoErrorCode.PERMISSION_ERROR)
{
}
public PermissionDeniedException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.PERMISSION_ERROR, transportDetails)
{
}
}
56 changes: 54 additions & 2 deletions src/Momento.Sdk/Exceptions/SdkException.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
using System;
using Grpc.Core;

namespace Momento.Sdk.Exceptions;

public enum MomentoErrorCode {
INVALID_ARGUMENT_ERROR,
UNKNOWN_SERVICE_ERROR,
ALREADY_EXISTS_ERROR,
NOT_FOUND_ERROR,
INTERNAL_SERVER_ERROR,
PERMISSION_ERROR,
AUTHENTICATION_ERROR,
CANCELLED_ERROR,
LIMIT_EXCEEDED_ERROR,
BAD_REQUEST_ERROR,
TIMEOUT_ERROR,
SERVER_UNAVAILABLE,
CLIENT_RESOURCE_EXHAUSTED,
FAILED_PRECONDITION_ERROR,
UNKNOWN_ERROR
}

public class MomentoGrpcErrorDetails {
public StatusCode code;
public string details;
public Metadata? metadata = null;

public MomentoGrpcErrorDetails(StatusCode code, string details, Metadata? metadata)
{
this.code = code;
this.details = details;
this.metadata = metadata;
}

}

public class MomentoErrorTransportDetails {
public MomentoGrpcErrorDetails grpc;

public MomentoErrorTransportDetails(MomentoGrpcErrorDetails grpc) {
this.grpc = grpc;
}
}

public abstract class SdkException : Exception
{
protected SdkException(string message) : base(message)
public MomentoErrorCode errorCode;
public MomentoErrorTransportDetails? transportDetails = null;

protected SdkException(string message, MomentoErrorCode errorCode) : base(message)
{
this.errorCode = errorCode;
}
protected SdkException(string message, MomentoErrorCode errorCode, MomentoErrorTransportDetails transportDetails) : base(message)
{
this.errorCode = errorCode;
this.transportDetails = transportDetails;
}
protected SdkException(string message, Exception e) : base(message, e)
protected SdkException(string message, MomentoErrorCode errorCode, MomentoErrorTransportDetails? transportDetails, Exception e) : base(message, e)
{
this.errorCode = errorCode;
this.transportDetails = transportDetails;
}
}
5 changes: 4 additions & 1 deletion src/Momento.Sdk/Exceptions/TimeoutException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class TimeoutException : SdkException
{
public TimeoutException(string message) : base(message)
public TimeoutException(string message) : base(message, MomentoErrorCode.TIMEOUT_ERROR)
{
}
public TimeoutException(string message, MomentoErrorTransportDetails transportDetails) : base(message, MomentoErrorCode.TIMEOUT_ERROR, transportDetails)
{
}
}
4 changes: 2 additions & 2 deletions src/Momento.Sdk/Exceptions/UnknownException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace Momento.Sdk.Exceptions;
/// </summary>
public class UnknownException : SdkException
{
public UnknownException(string message) : base(message)
public UnknownException(string message, MomentoErrorCode errorCode) : base(message, errorCode)
{
}
public UnknownException(string message, Exception e) : base(message, e)
public UnknownException(string message, Exception e) : base(message, MomentoErrorCode.UNKNOWN_ERROR, null, e)
{
}
}

0 comments on commit 16af95d

Please sign in to comment.