Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move pre-defined errors to constants #115

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Tochka.JsonRpc.Common/JsonRpcErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Tochka.JsonRpc.Common;

/// <summary>
/// Reserved predefined JSON-RPC error codes
/// </summary>
public static class JsonRpcErrorCodes
{
/// <summary>
/// Invalid JSON was received by the server.
/// An error occurred on the server while parsing the JSON text.
/// </summary>
public const int ParseError = -32700;

/// <summary>
/// The JSON sent is not a valid Request object
/// </summary>
public const int InvalidRequest = -32600;

/// <summary>
/// The method does not exist or is not available
/// </summary>
public const int MethodNotFound = -32601;

/// <summary>
/// Invalid method parameters
/// </summary>
public const int InvalidParams = -32602;

/// <summary>
/// Internal JSON-RPC error
/// </summary>
public const int InternalError = -32603;
}
6 changes: 6 additions & 0 deletions src/Tochka.JsonRpc.Common/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.JsonRpcErrorCodes.InternalError = -32603 -> int
const Tochka.JsonRpc.Common.JsonRpcErrorCodes.InvalidParams = -32602 -> int
const Tochka.JsonRpc.Common.JsonRpcErrorCodes.InvalidRequest = -32600 -> int
const Tochka.JsonRpc.Common.JsonRpcErrorCodes.MethodNotFound = -32601 -> int
const Tochka.JsonRpc.Common.JsonRpcErrorCodes.ParseError = -32700 -> int
Tochka.JsonRpc.Common.JsonRpcErrorCodes
16 changes: 10 additions & 6 deletions src/Tochka.JsonRpc.Server/Services/JsonRpcErrorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public JsonRpcErrorFactory(IOptions<JsonRpcServerOptions> options, ILogger<JsonR

/// <inheritdoc />
public IError ParseError(object? errorData) =>
new Error<object>(-32700, "Parse error", WrapExceptions(errorData));
new Error<object>(JsonRpcErrorCodes.ParseError, "Parse error", WrapExceptions(errorData));

/// <inheritdoc />
public IError InvalidRequest(object? errorData) =>
new Error<object>(-32600, "Invalid Request", WrapExceptions(errorData));
new Error<object>(JsonRpcErrorCodes.InvalidRequest, "Invalid Request", WrapExceptions(errorData));

/// <inheritdoc />
public IError MethodNotFound(object? errorData) =>
new Error<object>(-32601, "Method not found", WrapExceptions(errorData));
new Error<object>(JsonRpcErrorCodes.MethodNotFound, "Method not found", WrapExceptions(errorData));

/// <inheritdoc />
public IError InvalidParams(object? errorData) =>
new Error<object>(-32602, "Invalid params", WrapExceptions(errorData));
new Error<object>(JsonRpcErrorCodes.InvalidParams, "Invalid params", WrapExceptions(errorData));

/// <inheritdoc />
public IError InternalError(object? errorData) =>
new Error<object>(-32603, "Internal error", WrapExceptions(errorData));
new Error<object>(JsonRpcErrorCodes.InternalError, "Internal error", WrapExceptions(errorData));

/// <inheritdoc />
public IError ServerError(int code, object? errorData) =>
Expand Down Expand Up @@ -120,7 +120,11 @@ public virtual IError Error(int code, string message, object? errorData) =>
/// </summary>
/// <param name="code">error.code</param>
[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 JsonRpcErrorCodes.ParseError
or JsonRpcErrorCodes.InvalidRequest
or JsonRpcErrorCodes.MethodNotFound
or JsonRpcErrorCodes.InvalidParams
or JsonRpcErrorCodes.InternalError;

/// <summary>
/// Check if code is reserved for server implementation in specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ParseError_WrapException()

var result = errorFactoryMock.Object.ParseError(errorData);

var expected = new Error<object>(-32700, "Parse error", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.ParseError, "Parse error", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -56,7 +56,7 @@ public void InvalidRequest_WrapException()

var result = errorFactoryMock.Object.InvalidRequest(errorData);

var expected = new Error<object>(-32600, "Invalid Request", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InvalidRequest, "Invalid Request", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -72,7 +72,7 @@ public void MethodNotFound_WrapException()

var result = errorFactoryMock.Object.MethodNotFound(errorData);

var expected = new Error<object>(-32601, "Method not found", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.MethodNotFound, "Method not found", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -88,7 +88,7 @@ public void InvalidParams_WrapException()

var result = errorFactoryMock.Object.InvalidParams(errorData);

var expected = new Error<object>(-32602, "Invalid params", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InvalidParams, "Invalid params", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -104,7 +104,7 @@ public void InternalError_WrapException()

var result = errorFactoryMock.Object.InternalError(errorData);

var expected = new Error<object>(-32603, "Internal error", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InternalError, "Internal error", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public void Exception_JsonRpcMethodNotFoundException_ReturnMethodNotFoundError()

var result = errorFactoryMock.Object.Exception(exception);

var expected = new Error<object>(-32601, "Method not found", new { Method = methodName });
var expected = new Error<object>(JsonRpcErrorCodes.MethodNotFound, "Method not found", new { Method = methodName });
result.Should().BeEquivalentTo(expected);
}

Expand All @@ -241,7 +241,7 @@ public void Exception_JsonRpcFormatException_ReturnInvalidRequestError()

var result = errorFactoryMock.Object.Exception(exception);

var expected = new Error<object>(-32600, "Invalid Request", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InvalidRequest, "Invalid Request", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public void HttpError_HttpCode400Or422_ReturnInvalidParamsError(int httpCode)

var result = errorFactoryMock.Object.HttpError(httpCode, errorData);

var expected = new Error<object>(-32602, "Invalid params", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InvalidParams, "Invalid params", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -291,7 +291,7 @@ public void HttpError_HttpCode401Or403_ReturnMethodNotFoundError(int httpCode)

var result = errorFactoryMock.Object.HttpError(httpCode, errorData);

var expected = new Error<object>(-32601, "Method not found", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.MethodNotFound, "Method not found", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand Down Expand Up @@ -323,7 +323,7 @@ public void HttpError_HttpCode415_ReturnParseError()

var result = errorFactoryMock.Object.HttpError(415, errorData);

var expected = new Error<object>(-32700, "Parse error", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.ParseError, "Parse error", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand All @@ -339,7 +339,7 @@ public void HttpError_HttpCode500_ReturnInternalError()

var result = errorFactoryMock.Object.HttpError(500, errorData);

var expected = new Error<object>(-32603, "Internal error", wrappedData);
var expected = new Error<object>(JsonRpcErrorCodes.InternalError, "Internal error", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand Down
Loading