Coder Social home page Coder Social logo

Comments (6)

shubhamranjan avatar shubhamranjan commented on June 26, 2024

I totally agree on all the points mentioned. There's a lot of scope for refactoring. Thanks for pointing these out.

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

I have also changed 'throw ex' to simple 'throw' because 'throw ex' removes the original stack trace and creates a new one on the place of the throw while a simple 'throw' keeps the original stack trace that should provide more details. Especially with the helper function: without this change all stack traces would point to the helper function as the source of the exception.

@yoricksmeets Are you implementing these changes or should I proceed with their implementation ?

from dotnet-etcd.

yoricksmeets avatar yoricksmeets commented on June 26, 2024

@shubhamranjan I'm not implementing them at this moment. I can make some time later this week to implement these changes.

from dotnet-etcd.

yoricksmeets avatar yoricksmeets commented on June 26, 2024

I'm starting to work on this issue

from dotnet-etcd.

yoricksmeets avatar yoricksmeets commented on June 26, 2024

I arrived at the methods that use a AsyncDuplexStreamingCall and I was reading back through the issues why there is a errorHandler action in the function signature.
I think the problem raised in #34 is the reason this errorHandler action is included, but I think that if the watch operations return a Task instead of void, as pointed out in #40, it will give the user the option to catch the exception by handling the task that is returned by the method.

This setup will not return the exception that is thrown within the task so the caller has no way to handle the exception:

        [Test]
        public void TestVoidVsTask()
        {
            try
            {
                Watcher();
            }
            catch (Exception e)
            {
                // Never reached because Watcher() will never throw
            }
        }

        public async void Watcher()
        {
            while (true)
            {
                try
                {
                    Task watcherTask = Task.Run(async () =>
                    {
                        // stuff
                        throw new Exception();
                    });
                    await watcherTask;
                    return;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

The exception will now crash the entire program. And wrapping the call to 'Watcher()' in the test method with a try-catch will not help because the exception is not propagated.

This following code will propagate the error and gives the caller the option to handle the exceptions.

        [Test]
        public void TestVoidVsTask()
        {
            try
            {
                Watcher().GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                // Handle exception
            }
        }

        public async Task Watcher()
        {
            while (true)
            {
                try
                {
                    Task watcherTask = Task.Run(async () =>
                    {
                        // stuff
                        throw new Exception();
                    });
                    await watcherTask;
                    return;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

I was hoping to remove the exceptionHandler and change the return type from 'async void' to 'async Task' but this will break the API, so I would like your view on this matter @shubhamranjan

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

Yeah, the exception handler can be removed with return type changed to Task. We can do this change, a breaking change but not a feature removal. Will be fine.

from dotnet-etcd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.