Initial commit
This commit is contained in:
85
skills/assets/unity-package/Editor/Protocol/JsonRpcError.cs
Normal file
85
skills/assets/unity-package/Editor/Protocol/JsonRpcError.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace UnityEditorToolkit.Protocol
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON-RPC 2.0 Error object
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class JsonRpcError
|
||||
{
|
||||
[JsonProperty("code")]
|
||||
public int Code { get; set; }
|
||||
|
||||
[JsonProperty("message")]
|
||||
public string Message { get; set; }
|
||||
|
||||
[JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)]
|
||||
public object Data { get; set; }
|
||||
|
||||
public JsonRpcError() { }
|
||||
|
||||
public JsonRpcError(int code, string message, object data = null)
|
||||
{
|
||||
Code = code;
|
||||
Message = message;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
// Standard JSON-RPC 2.0 error codes
|
||||
public static readonly int PARSE_ERROR = -32700;
|
||||
public static readonly int INVALID_REQUEST = -32600;
|
||||
public static readonly int METHOD_NOT_FOUND = -32601;
|
||||
public static readonly int INVALID_PARAMS = -32602;
|
||||
public static readonly int INTERNAL_ERROR = -32603;
|
||||
|
||||
// Custom Unity error codes
|
||||
public static readonly int UNITY_NOT_CONNECTED = -32000;
|
||||
public static readonly int UNITY_COMMAND_FAILED = -32001;
|
||||
public static readonly int UNITY_OBJECT_NOT_FOUND = -32002;
|
||||
public static readonly int UNITY_SCENE_NOT_FOUND = -32003;
|
||||
public static readonly int UNITY_COMPONENT_NOT_FOUND = -32004;
|
||||
|
||||
// Factory methods for common errors
|
||||
public static JsonRpcError ParseError(string details = null)
|
||||
{
|
||||
return new JsonRpcError(PARSE_ERROR, "Parse error", details);
|
||||
}
|
||||
|
||||
public static JsonRpcError InvalidRequest(string details = null)
|
||||
{
|
||||
return new JsonRpcError(INVALID_REQUEST, "Invalid Request", details);
|
||||
}
|
||||
|
||||
public static JsonRpcError MethodNotFound(string method)
|
||||
{
|
||||
return new JsonRpcError(METHOD_NOT_FOUND, $"Method not found: {method}");
|
||||
}
|
||||
|
||||
public static JsonRpcError InvalidParams(string details)
|
||||
{
|
||||
return new JsonRpcError(INVALID_PARAMS, "Invalid params", details);
|
||||
}
|
||||
|
||||
public static JsonRpcError InternalError(string details)
|
||||
{
|
||||
return new JsonRpcError(INTERNAL_ERROR, "Internal error", details);
|
||||
}
|
||||
|
||||
public static JsonRpcError ObjectNotFound(string objectName)
|
||||
{
|
||||
return new JsonRpcError(UNITY_OBJECT_NOT_FOUND, $"GameObject not found: {objectName}");
|
||||
}
|
||||
|
||||
public static JsonRpcError SceneNotFound(string sceneName)
|
||||
{
|
||||
return new JsonRpcError(UNITY_SCENE_NOT_FOUND, $"Scene not found: {sceneName}");
|
||||
}
|
||||
|
||||
public static JsonRpcError CommandFailed(string details)
|
||||
{
|
||||
return new JsonRpcError(UNITY_COMMAND_FAILED, "Command failed", details);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d3776dd9cee1b44dac910779d8d4b5a
|
||||
108
skills/assets/unity-package/Editor/Protocol/JsonRpcRequest.cs
Normal file
108
skills/assets/unity-package/Editor/Protocol/JsonRpcRequest.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEditorToolkit.Editor.Utils;
|
||||
|
||||
namespace UnityEditorToolkit.Protocol
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON-RPC 2.0 Request
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class JsonRpcRequest
|
||||
{
|
||||
[JsonProperty("jsonrpc")]
|
||||
public string JsonRpc { get; set; } = "2.0";
|
||||
|
||||
[JsonProperty("id")]
|
||||
public object Id { get; set; }
|
||||
|
||||
[JsonProperty("method")]
|
||||
public string Method { get; set; }
|
||||
|
||||
[JsonProperty("params")]
|
||||
public JToken Params { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Context data (not serialized, used for passing runtime data like callbacks)
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
private Dictionary<string, object> context = new Dictionary<string, object>();
|
||||
|
||||
/// <summary>
|
||||
/// Get strongly-typed parameters (✅ 에러 처리 개선)
|
||||
/// </summary>
|
||||
public T GetParams<T>() where T : class
|
||||
{
|
||||
if (Params == null || Params.Type == JTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Params.ToObject<T>();
|
||||
}
|
||||
catch (Newtonsoft.Json.JsonException ex)
|
||||
{
|
||||
// JSON 역직렬화 실패 시 예외를 다시 던져서 호출자가 처리하도록
|
||||
ToolkitLogger.LogError("JsonRpcRequest", $"Failed to deserialize params to {typeof(T).Name}: {ex.Message}");
|
||||
throw new ArgumentException($"Invalid parameter format for {typeof(T).Name}: {ex.Message}", ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 기타 예외도 동일하게 처리
|
||||
ToolkitLogger.LogError("JsonRpcRequest", $"Unexpected error deserializing params: {ex.Message}");
|
||||
throw new ArgumentException($"Failed to deserialize parameters: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set context data
|
||||
/// </summary>
|
||||
public void SetContext<T>(string key, T value)
|
||||
{
|
||||
context[key] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get context data
|
||||
/// </summary>
|
||||
public T GetContext<T>(string key)
|
||||
{
|
||||
if (context.TryGetValue(key, out var value))
|
||||
{
|
||||
return (T)value;
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if request is valid
|
||||
/// </summary>
|
||||
public bool IsValid()
|
||||
{
|
||||
return JsonRpc == "2.0" && !string.IsNullOrEmpty(Method);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize request to JSON string
|
||||
/// </summary>
|
||||
public string ToJson()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize JSON string to request object
|
||||
/// </summary>
|
||||
public static JsonRpcRequest FromJson(string json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<JsonRpcRequest>(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d22a22ac1750064e83decd79852fd00
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace UnityEditorToolkit.Protocol
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON-RPC 2.0 Success Response
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class JsonRpcResponse
|
||||
{
|
||||
[JsonProperty("jsonrpc")]
|
||||
public string JsonRpc { get; set; } = "2.0";
|
||||
|
||||
[JsonProperty("id")]
|
||||
public object Id { get; set; }
|
||||
|
||||
[JsonProperty("result")]
|
||||
public object Result { get; set; }
|
||||
|
||||
public JsonRpcResponse() { }
|
||||
|
||||
public JsonRpcResponse(object id, object result)
|
||||
{
|
||||
Id = id;
|
||||
Result = result;
|
||||
}
|
||||
|
||||
public string ToJson()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON-RPC 2.0 Error Response
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class JsonRpcErrorResponse
|
||||
{
|
||||
[JsonProperty("jsonrpc")]
|
||||
public string JsonRpc { get; set; } = "2.0";
|
||||
|
||||
[JsonProperty("id")]
|
||||
public object Id { get; set; }
|
||||
|
||||
[JsonProperty("error")]
|
||||
public JsonRpcError Error { get; set; }
|
||||
|
||||
public JsonRpcErrorResponse() { }
|
||||
|
||||
public JsonRpcErrorResponse(object id, JsonRpcError error)
|
||||
{
|
||||
Id = id;
|
||||
Error = error;
|
||||
}
|
||||
|
||||
public string ToJson()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbe717f81e543a846991ac07b8ecda70
|
||||
Reference in New Issue
Block a user