msemys / esjc

EventStore Java Client
MIT License
108 stars 27 forks source link

How to handle non existing streams #34

Closed olijaun closed 5 years ago

olijaun commented 6 years ago

Hi

Thanks for the great library!

I'm trying to implement my first event sourcing application using DDD. I have a repository with a get(id) method. In this method I try to load an aggregate by id (each stream represents an aggregate). If the stream does not exist I want to return 'null'. However with eventStore.streamEventsForward() I get "java.lang.IllegalStateException: Unexpected read status: StreamNotFound".

I would like this method to return a specific exception. Is this intentionally implemented this way? An additional check to see whether the stream exists would cause another roundtrip which I want to avoid.

Could you throw a specific exception? Or do you want me to create a pull request?

Heres my code: `List domainEventList = null; try { domainEventList = eventStore.streamEventsForward(streamId.getValue(), 0, 4096, false) // .map(e -> toObject(e)).collect(Collectors.toList());

    } catch (Exception e) {
        return null;
    }`

As you can see I'm returning null if I get an exception, but this is just a workaround.

Regards Oliver

msemys commented 6 years ago

I think it would be better to improve stream iterators/splitterators, that could return Collections.emptyIterator() or Stream.empty() in case of stream not found or deleted.

other options at the moment could be:

StreamEventsSlice slice; long position = StreamPosition.START;

do { slice = eventstore.readStreamEventsForward("stream", position, 1028, false).join(); events.addAll(slice.events); position = slice.nextEventNumber; } while (!slice.isEndOfStream);

olijaun commented 6 years ago

Thanks for the hints I will try that. Of course your proposed solution regarding iterators/splitterators would be fine. Thanks! Regards Oliver

msemys commented 5 years ago

now throws specific exceptions StreamNotFoundException or StreamDeletedException instead of IllegalStateException