Skip to content

Commit

Permalink
feat: move pre-defined errors to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ekonyukov committed Aug 26, 2024
1 parent 2c0dc73 commit 919cca2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
33 changes: 33 additions & 0 deletions src/Tochka.JsonRpc.Common/JsonRpcPreDefinedErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Tochka.JsonRpc.Common;

/// <summary>
/// Reserved pre-defined JSON-RPC error codes
/// </summary>
public static class JsonRpcPreDefinedErrorCodes
{
/// <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.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
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>(JsonRpcPreDefinedErrorCodes.ParseError, "Parse error", WrapExceptions(errorData));

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

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

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

/// <inheritdoc />
public IError InternalError(object? errorData) =>
new Error<object>(-32603, "Internal error", WrapExceptions(errorData));
new Error<object>(JsonRpcPreDefinedErrorCodes.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 JsonRpcPreDefinedErrorCodes.ParseError
or JsonRpcPreDefinedErrorCodes.InvalidRequest
or JsonRpcPreDefinedErrorCodes.MethodNotFound
or JsonRpcPreDefinedErrorCodes.InvalidParams
or JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.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>(JsonRpcPreDefinedErrorCodes.InternalError, "Internal error", wrappedData);
result.Should().BeEquivalentTo(expected);
errorFactoryMock.Verify();
}
Expand Down

0 comments on commit 919cca2

Please sign in to comment.