diff --git a/src/Tochka.JsonRpc.Common/JsonRpcPreDefinedErrorCodes.cs b/src/Tochka.JsonRpc.Common/JsonRpcPreDefinedErrorCodes.cs new file mode 100644 index 0000000..9a732bf --- /dev/null +++ b/src/Tochka.JsonRpc.Common/JsonRpcPreDefinedErrorCodes.cs @@ -0,0 +1,33 @@ +namespace Tochka.JsonRpc.Common; + +/// +/// Reserved pre-defined JSON-RPC error codes +/// +public static class JsonRpcPreDefinedErrorCodes +{ + /// + /// Invalid JSON was received by the server. + /// An error occurred on the server while parsing the JSON text. + /// + public const int ParseError = -32700; + + /// + /// The JSON sent is not a valid Request object + /// + public const int InvalidRequest = -32600; + + /// + /// The method does not exist or is not available + /// + public const int MethodNotFound = -32601; + + /// + /// Invalid method parameters + /// + public const int InvalidParams = -32602; + + /// + /// Internal JSON-RPC error + /// + public const int InternalError = -32603; +} diff --git a/src/Tochka.JsonRpc.Common/PublicAPI.Shipped.txt b/src/Tochka.JsonRpc.Common/PublicAPI.Shipped.txt index 9ef0728..e4f40b5 100644 --- a/src/Tochka.JsonRpc.Common/PublicAPI.Shipped.txt +++ b/src/Tochka.JsonRpc.Common/PublicAPI.Shipped.txt @@ -177,3 +177,9 @@ Tochka.JsonRpc.Common.Features.JsonRpcFeature.RawCall.set -> void Tochka.JsonRpc.Common.Features.JsonRpcFeature.Response.get -> Tochka.JsonRpc.Common.Models.Response.IResponse? Tochka.JsonRpc.Common.Features.JsonRpcFeature.Response.set -> void const Tochka.JsonRpc.Common.JsonRpcConstants.OutgoingHttpRequestOptionMethodNameKey = "tochka_outgoing_http_request_method_name" -> string! +const Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes.InternalError = -32603 -> int +const Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes.InvalidParams = -32602 -> int +const Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes.InvalidRequest = -32600 -> int +const Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes.MethodNotFound = -32601 -> int +const Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes.ParseError = -32700 -> int +Tochka.JsonRpc.Common.JsonRpcPreDefinedErrorCodes diff --git a/src/Tochka.JsonRpc.Server/Services/JsonRpcErrorFactory.cs b/src/Tochka.JsonRpc.Server/Services/JsonRpcErrorFactory.cs index 083408b..87aa4c1 100644 --- a/src/Tochka.JsonRpc.Server/Services/JsonRpcErrorFactory.cs +++ b/src/Tochka.JsonRpc.Server/Services/JsonRpcErrorFactory.cs @@ -25,23 +25,23 @@ public JsonRpcErrorFactory(IOptions options, ILogger public IError ParseError(object? errorData) => - new Error(-32700, "Parse error", WrapExceptions(errorData)); + new Error(JsonRpcPreDefinedErrorCodes.ParseError, "Parse error", WrapExceptions(errorData)); /// public IError InvalidRequest(object? errorData) => - new Error(-32600, "Invalid Request", WrapExceptions(errorData)); + new Error(JsonRpcPreDefinedErrorCodes.InvalidRequest, "Invalid Request", WrapExceptions(errorData)); /// public IError MethodNotFound(object? errorData) => - new Error(-32601, "Method not found", WrapExceptions(errorData)); + new Error(JsonRpcPreDefinedErrorCodes.MethodNotFound, "Method not found", WrapExceptions(errorData)); /// public IError InvalidParams(object? errorData) => - new Error(-32602, "Invalid params", WrapExceptions(errorData)); + new Error(JsonRpcPreDefinedErrorCodes.InvalidParams, "Invalid params", WrapExceptions(errorData)); /// public IError InternalError(object? errorData) => - new Error(-32603, "Internal error", WrapExceptions(errorData)); + new Error(JsonRpcPreDefinedErrorCodes.InternalError, "Internal error", WrapExceptions(errorData)); /// public IError ServerError(int code, object? errorData) => @@ -120,7 +120,11 @@ public virtual IError Error(int code, string message, object? errorData) => /// /// error.code [ExcludeFromCodeCoverage] - protected static bool IsSpecial(int code) => code is -32700 or -32600 or -32601 or -32602 or -32603; + protected static bool IsSpecial(int code) => code is JsonRpcPreDefinedErrorCodes.ParseError + or JsonRpcPreDefinedErrorCodes.InvalidRequest + or JsonRpcPreDefinedErrorCodes.MethodNotFound + or JsonRpcPreDefinedErrorCodes.InvalidParams + or JsonRpcPreDefinedErrorCodes.InternalError; /// /// Check if code is reserved for server implementation in specification diff --git a/src/tests/Tochka.JsonRpc.Server.Tests/Services/JsonRpcErrorFactoryTests.cs b/src/tests/Tochka.JsonRpc.Server.Tests/Services/JsonRpcErrorFactoryTests.cs index 339dbe5..e4308e0 100644 --- a/src/tests/Tochka.JsonRpc.Server.Tests/Services/JsonRpcErrorFactoryTests.cs +++ b/src/tests/Tochka.JsonRpc.Server.Tests/Services/JsonRpcErrorFactoryTests.cs @@ -40,7 +40,7 @@ public void ParseError_WrapException() var result = errorFactoryMock.Object.ParseError(errorData); - var expected = new Error(-32700, "Parse error", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.ParseError, "Parse error", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -56,7 +56,7 @@ public void InvalidRequest_WrapException() var result = errorFactoryMock.Object.InvalidRequest(errorData); - var expected = new Error(-32600, "Invalid Request", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InvalidRequest, "Invalid Request", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -72,7 +72,7 @@ public void MethodNotFound_WrapException() var result = errorFactoryMock.Object.MethodNotFound(errorData); - var expected = new Error(-32601, "Method not found", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.MethodNotFound, "Method not found", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -88,7 +88,7 @@ public void InvalidParams_WrapException() var result = errorFactoryMock.Object.InvalidParams(errorData); - var expected = new Error(-32602, "Invalid params", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InvalidParams, "Invalid params", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -104,7 +104,7 @@ public void InternalError_WrapException() var result = errorFactoryMock.Object.InternalError(errorData); - var expected = new Error(-32603, "Internal error", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InternalError, "Internal error", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -215,7 +215,7 @@ public void Exception_JsonRpcMethodNotFoundException_ReturnMethodNotFoundError() var result = errorFactoryMock.Object.Exception(exception); - var expected = new Error(-32601, "Method not found", new { Method = methodName }); + var expected = new Error(JsonRpcPreDefinedErrorCodes.MethodNotFound, "Method not found", new { Method = methodName }); result.Should().BeEquivalentTo(expected); } @@ -241,7 +241,7 @@ public void Exception_JsonRpcFormatException_ReturnInvalidRequestError() var result = errorFactoryMock.Object.Exception(exception); - var expected = new Error(-32600, "Invalid Request", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InvalidRequest, "Invalid Request", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -274,7 +274,7 @@ public void HttpError_HttpCode400Or422_ReturnInvalidParamsError(int httpCode) var result = errorFactoryMock.Object.HttpError(httpCode, errorData); - var expected = new Error(-32602, "Invalid params", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InvalidParams, "Invalid params", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -291,7 +291,7 @@ public void HttpError_HttpCode401Or403_ReturnMethodNotFoundError(int httpCode) var result = errorFactoryMock.Object.HttpError(httpCode, errorData); - var expected = new Error(-32601, "Method not found", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.MethodNotFound, "Method not found", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -323,7 +323,7 @@ public void HttpError_HttpCode415_ReturnParseError() var result = errorFactoryMock.Object.HttpError(415, errorData); - var expected = new Error(-32700, "Parse error", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.ParseError, "Parse error", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); } @@ -339,7 +339,7 @@ public void HttpError_HttpCode500_ReturnInternalError() var result = errorFactoryMock.Object.HttpError(500, errorData); - var expected = new Error(-32603, "Internal error", wrappedData); + var expected = new Error(JsonRpcPreDefinedErrorCodes.InternalError, "Internal error", wrappedData); result.Should().BeEquivalentTo(expected); errorFactoryMock.Verify(); }