EventStore / EventStoreDB-Client-Java

Official Asynchronous Java 8+ Client Library for EventStoreDB 20.6+
https://eventstore.com
Apache License 2.0
63 stars 20 forks source link

V4.0.0 - WrongExpectedVersionException does not allow to get actual position #203

Closed TomMante closed 1 year ago

TomMante commented 1 year ago

We are currently trying to move our code to version 4.0.0 from version 3.0.1. In Version 4.0.0 you guys split the StreamRevision into two separate Concepts: ExpectedRevision and StreamPosition.

We are translating the exception: WrongExpectedVersionException. The issue is that we used to print out the revision Number (long) value. The way it is built now, both fields : nextExpectedRevisionUnsigned, actualRevision

Are ExpectedRevision, and from this class we can not extract the actual version. Therefore we are not able to extract the "actualRevision".

First of all shouldn't "actualRevision" be a StreamPosition?

Second, from the way ExpectedRevision is implemented, it seems impossible for a user to get the actual ExpectedRevision value.

It is possible, although a bit tedious, via equals to figure out which subtype of Expected revision it is, but not to get the actual revision number that it's expected.

In most cases this isn't an issue, because if you are expecting a Revision you already know the revision value, but in the case of the exception this is an issue.

So my question is: would it be possible to either change the WrongExpectedVersionException to return StreamPositions instead of ExpectedRevisions, or, in alternative, to be able to get a StreamPosition out of an ExpectedRevision?

If neither is possible, what would you suggest as an approach?

Thx,

Regards,

Thomas

YoEight commented 1 year ago

Hey @TomMante

Let's start from a code snippet of what would be your ideal usage and see what we can do from there.

What do you think?

TomMante commented 1 year ago

Hi, thx for the reply.

So we have some code that prints an error message, that provides us with some information regarding the version.

Before 4.0.0 I was doing something like this:

WrongExpectedVersionException error -> new WrongExpectedVersion(
                                error.getStreamName(),
                                error.getActualVersion().getValueUnsigned(),
                                error.getNextExpectedRevision().getValueUnsigned()
                        )

With:

public class WrongExpectedVersion extends RuntimeException {
    public WrongExpectedVersion(String stream, Number actualRevision, Number nextExpectedRevision) {
        super(format("Error while appending events in stream '%s' : actual stream revision = %s nextExpectedRevision = %s", stream, actualRevision, nextExpectedRevision));
    }
}

With the current implementation I can not extrapolate such information. So what I would like ideally is to still be able to get the value of the actual Version and NextExpectedRevision from WrongExpectedVersionException.

If this is not feasible, it would be cool to get at least a "toString" method to print out the string representation of The ExpectedVersion.

Let me know if this makes sense, or if you think I should get this information some other way.

Thank you

YoEight commented 1 year ago

I created a PR, please share your feedback!

TomMante commented 1 year ago

@YoEight Thank you very much, I think this will solve our problem!