Skip to content

Commit

Permalink
feat: Enhanced TxApplied subscription to show success/failure details
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa committed Jul 31, 2023
1 parent 34b6242 commit 91f4370
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 73 deletions.
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/BuyCookingEquipmentAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Savor22b.Action;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.Constants;
using Savor22b.Helpers;
using Savor22b.Model;
Expand Down
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/BuyRandomSeedItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Savor22b.Action;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.Constants;
using Savor22b.Helpers;
using Savor22b.Model;
Expand Down
83 changes: 83 additions & 0 deletions backend/app/Savor22b/Action/Exceptions/ActionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Runtime.Serialization;
using Bencodex.Types;
using Libplanet.Action;

[Serializable]
public class ActionException : Exception, IExtractableException, ISerializable
{
// 새로운 예외 클래스에서 사용할 메타데이터 프로퍼티
private readonly string ErrorType;
private readonly string ErrorMessage;
private readonly int? ErrorCode;
public IValue Metadata { get; }

public ActionException(string message, string errorType, int? errorCode)
: base(message)
{
ErrorType = errorType;
ErrorMessage = message;
ErrorCode = errorCode;

var metaData = new Dictionary(
new[]
{
new KeyValuePair<IKey, IValue>(
(Text) "errorType",
(Text) errorType
),
new KeyValuePair<IKey, IValue>(
(Text) "errorMessage",
(Text) message
)
}
);

if (errorCode is int code)
{
Metadata = metaData.SetItem(
(Text)"errorCode",
(Integer)code
);
}

Metadata = metaData;

}
protected ActionException(SerializationInfo info, StreamingContext context)

Check warning on line 46 in backend/app/Savor22b/Action/Exceptions/ActionException.cs

View workflow job for this annotation

GitHub Actions / build-and-tests

Non-nullable field 'ErrorType' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 46 in backend/app/Savor22b/Action/Exceptions/ActionException.cs

View workflow job for this annotation

GitHub Actions / build-and-tests

Non-nullable field 'ErrorMessage' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 46 in backend/app/Savor22b/Action/Exceptions/ActionException.cs

View workflow job for this annotation

GitHub Actions / build-and-tests

Non-nullable property 'Metadata' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
: base(info, context)
{
if (info.GetString(nameof(ErrorType)) is { } errorType)
{
ErrorType = errorType;
}

if (info.GetString(nameof(ErrorMessage)) is { } errorMessage)
{
ErrorMessage = errorMessage;
}

if (info.GetInt32(nameof(ErrorCode)) is int errorCode)
{
ErrorCode = errorCode;
}

if (info.GetValue(nameof(Metadata), typeof(IValue)) is IValue metadata)
{
Metadata = metadata;
}
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);

info.AddValue(nameof(ErrorType), ErrorType);
info.AddValue(nameof(ErrorMessage), ErrorMessage);
info.AddValue(nameof(ErrorCode), ErrorCode);

if (Metadata is { } metadata)
{
info.AddValue(nameof(Metadata), metadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Savor22b.Action.Exceptions;


[Serializable]
public class HouseAlreadyPlacedException : ActionException
{
public HouseAlreadyPlacedException(string message, int? errorCode = null)
: base(message, "HouseAlreadyPlaced", errorCode)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Savor22b.Action;
namespace Savor22b.Action.Exceptions;

using System.Runtime.Serialization;
using Libplanet;
Expand Down
11 changes: 11 additions & 0 deletions backend/app/Savor22b/Action/Exceptions/InvalidVillageException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Savor22b.Action.Exceptions;


[Serializable]
public class InvalidVillageException : ActionException
{
public InvalidVillageException(string message, int? errorCode = null)
: base(message, "InvalidVillage", errorCode)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Savor22b.Action.Exceptions;

using System.Runtime.Serialization;


[Serializable]
public class NotEnoughRawMaterialsException : ActionException
{
public NotEnoughRawMaterialsException(string message, int? errorCode = null)
: base(message, "NotEnoughRawMaterials", errorCode)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Savor22b.Action.Exceptions;


[Serializable]
public class NotFoundTableDataException : ActionException
{
public NotFoundTableDataException(string message, int? errorCode = null)
: base(message, "NotFoundTableData", errorCode)
{
}
}
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/GenerateFoodAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Savor22b.Action;
using Libplanet.Action;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.Helpers;
using Savor22b.Model;
using Savor22b.States;
Expand Down
22 changes: 0 additions & 22 deletions backend/app/Savor22b/Action/NotEnoughRawMaterialsException.cs

This file was deleted.

22 changes: 0 additions & 22 deletions backend/app/Savor22b/Action/NotFoundTableDataException.cs

This file was deleted.

7 changes: 4 additions & 3 deletions backend/app/Savor22b/Action/PlaceUserHouseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Savor22b.Action;
using Libplanet.Headless.Extensions;
using Savor22b.Constants;
using Libplanet;
using Savor22b.Action.Exceptions;

[ActionType(nameof(PlaceUserHouseAction))]
public class PlaceUserHouseAction : SVRAction
Expand Down Expand Up @@ -84,12 +85,12 @@ public override IAccountStateDelta Execute(IActionContext ctx)

if (village == null)
{
throw new ArgumentException("Invalid village ID");
throw new InvalidVillageException("Invalid village ID");
}

if (!isAbleToPlaceHouse(village, TargetX, TargetY))
{
throw new ArgumentException("Invalid target position");
throw new InvalidVillageException("Invalid target position");
}

GlobalUserHouseState globalUserHouseState =
Expand All @@ -101,7 +102,7 @@ public override IAccountStateDelta Execute(IActionContext ctx)

if (globalUserHouseState.UserHouse.ContainsKey(userHouseKey))
{
throw new ArgumentException("House already placed");
throw new HouseAlreadyPlacedException("House already placed");
}

RootState rootState = states.GetState(ctx.Signer) is Bencodex.Types.Dictionary rootStateEncoded
Expand Down
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/TransferAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Savor22b.Action;
using Libplanet.State;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Savor22b.Action.Exceptions;

/// <summary>
/// Basically, it's just a double of <see cref="Libplanet.Action.Sys.Tranfer"/>,
Expand Down
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/UseRandomSeedItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Savor22b.Action;
using Libplanet.Action;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.Helpers;
using Savor22b.Model;
using Savor22b.States;
Expand Down
40 changes: 40 additions & 0 deletions backend/app/Savor22b/GraphTypes/ExceptionMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Savor22b.GraphTypes;

using Bencodex.Types;
using Libplanet.Headless.Extensions;

public class ExceptionMetadata
{
public string errorType { get; set; }
public string errorMessage { get; set; }
public int? errorCode { get; set; }

public ExceptionMetadata(string errorType, string errorMessage, int? errorCode)
{
this.errorType = errorType;
this.errorMessage = errorMessage;
this.errorCode = errorCode;
}

public ExceptionMetadata(Bencodex.Types.IValue encoded)
{
if (encoded is Bencodex.Types.Dictionary dict)
{
// Bencodex 데이터에서 필요한 값을 꺼내서 ExceptionMetadata 객체에 매핑합니다.
if (dict.TryGetValue((Text)"errorType", out var errorTypeValue) && errorTypeValue is Text errorTypeText)
{
errorType = errorTypeText.Value;
}

if (dict.TryGetValue((Text)"errorMessage", out var errorMessageValue) && errorMessageValue is Text errorMessageText)
{
errorMessage = errorMessageText.Value;
}

if (dict.TryGetValue((Text)"errorCode", out var errorCodeValue) && errorCodeValue is Integer errorCodeInteger)
{
errorCode = (int)errorCodeInteger;
}
}
}
}
28 changes: 28 additions & 0 deletions backend/app/Savor22b/GraphTypes/ExceptionMetadataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Bencodex.Types;
using GraphQL.Types;
using Savor22b.GraphTypes;


public class ExceptionMetadataType : ObjectGraphType<ExceptionMetadata>
{
public ExceptionMetadataType()
{
Field<NonNullGraphType<StringGraphType>>(
name: "errorType",
description: "The error type of the exception.",
resolve: context => context.Source.errorType
);

Field<NonNullGraphType<StringGraphType>>(
name: "errorMessage",
description: "The error message of the exception.",
resolve: context => context.Source.errorMessage
);

Field<IntGraphType>(
name: "errorCode",
description: "The error code of the exception.",
resolve: context => context.Source.errorCode
);
}
}
Loading

0 comments on commit 91f4370

Please sign in to comment.