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

java: don't use checked exceptions for our own programming errors #1423

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

matklad
Copy link
Member

@matklad matklad commented Jan 12, 2024

RequestException is thrown when the underlying C library detects that
the request is invalid --- the op or the size of message body is wrong.

However, the public API of of our Java client ensures that these are
always correct: it is impossible to trigger these errors unless:

  • there's a bug in our C or Java code
  • there's a version mismatch between our Java and C code (which is
    unlikely, as they come from the same jar)

So, there's absolutely no reason to tax the caller with declaring and
handling this exception.

RequestException is thrown when the underlying C library detects that
the request is invalid --- the op or the size of message body is wrong.

However, the public API of of our Java client ensures that these are
always correct: it is impossible to trigger these errors unless:

* there's a bug in our C or Java code
* there's a version mismatch between our Java and C code (which is
  unlikely, as they come from the same jar)

So, there's absolutely no reason to tax the caller with declaring and
handling this exception.
else if (status == PacketStatus.InvalidDataSize.value)
return "Invalid data size. Check if this client is compatible with the server's version.";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, this is just wrong.

@@ -143,7 +143,7 @@ private void waitForCompletionUninterruptibly() {
}
}

TResponse getResult() throws RequestException {
TResponse getResult() {

assertTrue(result != null || exception != null, "Unexpected request result: result=null");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering... Do we actually need this exception as a separate class even? From that

Check if this client is compatible with the server's version.

It feels like the original intention behind the exception was to signal error response from the server, and then it mutated to signal local programming errors. In this new role, perhaps we can just throw an AssertionError for non-ok packet statuses?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original intention was to map result codes into Java exceptions: InitializationException for tb_client_init, RequestException for packet.status, and ConcurrencyExceededException for tb_client_acquire_packet.

Check if this client is compatible with the server's version.

This message is indeed wrong, we never planned to return error response from the server at the protocol level (e.g. invalid size or operation), I think the C# client uses the same string.

(...)

I completely agree with RequestException extends RuntimeException, but I don't have a strong opinion on using AssertionError for it (feels like we are losing information if we reduce the packet status to an assertion error message).

@matklad matklad marked this pull request as ready for review January 13, 2024 11:50
@@ -143,7 +143,7 @@ private void waitForCompletionUninterruptibly() {
}
}

TResponse getResult() throws RequestException {
TResponse getResult() {

assertTrue(result != null || exception != null, "Unexpected request result: result=null");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original intention was to map result codes into Java exceptions: InitializationException for tb_client_init, RequestException for packet.status, and ConcurrencyExceededException for tb_client_acquire_packet.

Check if this client is compatible with the server's version.

This message is indeed wrong, we never planned to return error response from the server at the protocol level (e.g. invalid size or operation), I think the C# client uses the same string.

(...)

I completely agree with RequestException extends RuntimeException, but I don't have a strong opinion on using AssertionError for it (feels like we are losing information if we reduce the packet status to an assertion error message).

@matklad matklad added this pull request to the merge queue Jan 15, 2024
@matklad
Copy link
Member Author

matklad commented Jan 15, 2024

I don't have a strong opinion on using AssertionError for it (feels like we are losing information if we reduce the packet status to an assertion error message).

Yeah, same here. The argument "for" removing the dedicated exception is reducing the public API surface area by one class.

I guess:

  • it's a small mistake either way
  • while things are still in flux, let's use a dedicated exception
  • when we start giving backwards compatability guarantees about the API, I'd maybe feel moderately strongly about not exposing this exception, but we are not at that point yet.

Merged via the queue into main with commit 25840d2 Jan 15, 2024
25 checks passed
@matklad matklad deleted the matklad/docs-generate-5 branch January 15, 2024 11:25
@batiati
Copy link
Contributor

batiati commented Jan 15, 2024

I agree. Let's wait for the client refactoring King is working on. I think ConcurrencyExceededException will be dropped as well.

Yeah, same here. The argument "for" removing the dedicated exception is reducing the public API surface area by one class.

Additionally:

  • If we were implementing the Java Client 100% in pure Java, RequestException wouldn't exist.

  • AssertionError is used when the Java side has a bug and any other result from the native library is mapped by a specialized exception. In this case, the kind of results RequestException maps aren't expected to happen unless there's a bug on the Java side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants