Coder Social home page Coder Social logo

Comments (4)

spericas avatar spericas commented on June 12, 2024

There's one way to support a similar use case using existing features in Rest Client and Fault Tolerance. Rest Client has the notion of a ResponseExceptionMapper, which can be used to map a Response to an Exception. For example,

    public class MyResponseMapper implements ResponseExceptionMapper<MyException> {

        @Override
        public MyException toThrowable(Response response) {
            if (response.getStatus() != 200) {
                System.out.println("Throwing exception");
                throw new MyException(response.getStatus());
            }
            return null;
        }
    }

In this example, and mainly for simplicity, any status code other than 200 is mapped to an instance of MyException (naturally, different error codes could be mapped to different exception types). This mapper can be registered as a provider in a client and used with the normal FT @Retry:

    @Path("/greet")
    @RegisterRestClient(baseUri="http://localhost:8080")
    @RegisterProvider(MyResponseMapper.class)
    public interface GreetResourceClient {

        @GET
        @Retry(retryOn = { MyException.class })
        @Produces(MediaType.APPLICATION_JSON)
        Response getDefaultMessage();
    }

Thus, any time MyException is thrown, the exception will be caught by the FT handler and the method retried up to 3 times (the default for that annotation).

I understand this may not be as convenient as having a dedicated annotation for this purpose, but it is possible using existing constructs and extensions.

from helidon.

spericas avatar spericas commented on June 12, 2024

@sammee Have you tried the proposed alternative?

from helidon.

sammee avatar sammee commented on June 12, 2024

@spericas I have implemented a variant of this. Using ResponseMapper will throw a custom exception for a particular status code. This will be applied for all Rest clients. Instead, I wanted to have retry on some specific api. So i have used @Retry annotation on the method where the api is called and throw a custom exception when the status-code or error is retryable.
Thanks for the suggestion.

from helidon.

spericas avatar spericas commented on June 12, 2024

I gather from the comments that the workaround or alternative is satisfactory. Closing as won't fix for now.

from helidon.

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.