question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question // "orElse" route

See original GitHub issue

The code I have until now in my application is the following (in Kotlin, I hope it is still understandable).

            getDispatcher(http)
                .dispatch(Navigators.series(),
                    Bindings.on(HttpStatus.Series.SUCCESSFUL).call { response ->
                        deferred.resolve(Response(response, extract(response, clazz)))
                    },
                    Bindings.anySeries().dispatch(
                        Navigators.status(),
                        *errorBindings,
                        Bindings.anyStatus().call(ProblemRoute.problemHandling {
                            deferred.reject(it as Exception)
                        })
                    )
                )
                .join()

The problem I have here is that if the error that is returned is not of one of the content-type managed by ProblemRoute.problemHandling, the complete call fails with a Riptide exception about no route being defined for this case.

I am looking for a way to add something after the use of problemHandling that would semantically means: “If at this point, there was no route defined for this case, do <this>”.

For reference, here is the code for problemHandling:

        final Route route = Route.call(Exceptional.class, consumer);
        return dispatch(contentType(),
                on(PROBLEM).call(route),
                on(X_DOT_PROBLEM).call(route),
                on(X_DASH_PROBLEM).call(route));

The way I understand it, I would need to insert myself as a last parameter inside of that call to dispatch (after on(X_DASH_PROBLEM.call(route)).

To make my case work, I had to import some code from ProblemRoute into my own application and do something like this:

// some static stuff
        private val PROBLEM = parseMediaType("application/problem+json")
        private val X_DOT_PROBLEM = parseMediaType("application/x.problem+json")
        private val X_DASH_PROBLEM = parseMediaType("application/x-problem+json")
// and then, in my method
            val problemJsonRoute = Route.call(Exceptional::class.java) {
                deferred.reject(it as Exception)
            }
            val orElseConsumer: (ClientHttpResponse) -> Unit = {
                deferred.reject(Exception(it.body.bufferedReader().readText()))
            }
            getDispatcher(http)
                .dispatch(
                    Navigators.series(),
                    Bindings.on(SUCCESSFUL).call { response ->
                        deferred.resolve(Response(response, extract(response, clazz)))
                    },
                    Bindings.anySeries().call(RoutingTree.dispatch(
                        Navigators.status(),
                        *errorBindings,
                        Bindings.anyStatus().call(dispatch<MediaType>(
                            contentType(),
                            on<MediaType>(PROBLEM).call(problemJsonRoute),
                            on<MediaType>(X_DOT_PROBLEM).call(problemJsonRoute),
                            on<MediaType>(X_DASH_PROBLEM).call(problemJsonRoute),
                            Bindings.anyContentType().call(orElseConsumer)
                        ))
                    ))
                )
                .join()

It works and I could just go on like this, but I feel that is a shame to not use existing library code (ProblemRoute).

Is there a “clean” way of doing what I want? I am quite new to Riptide and might not understand correctly the dispatching/routing system.

Thanks for any assistance you can provide!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
whiskeysierracommented, Jun 20, 2017

I opened #206. @Adeynack Can you take a look if this would work for you?

1reaction
whiskeysierracommented, Jun 20, 2017

@Adeynack Thanks for bringing this up!

It works and I could just go on like this, but I feel that is a shame to not use existing library code (ProblemRoute).

I 100% agree. Providing a fallback like this totally makes sense.

I am quite new to Riptide and might not understand correctly the dispatching/routing system.

Based on your code samples and observations I’d say you have an extremely good grasp of it.

@lukasniemeier-zalando

In general one can think of making problemHandling extensible @whiskeysierra

I already have a draft in the works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional orElse() method in Java with examples
Parameters: This method accepts value as a parameter of type T to return if there is no value present in this Optional instance....
Read more >
java.util.Optional.orElseThrow java code examples - Tabnine
Best Java code snippets using java.util.Optional.orElseThrow (Showing top 20 results out of 18,099) · Javadoc · Popular methods of Optional · Popular in...
Read more >
Cast from Optional<> to ArrayList<> - java - Stack Overflow
So, my problem is that b.c.test() returns a value with Optional<A> as return type. But I need to return an ArrayList<A> . So,...
Read more >
Route Templates | Additional Guides | Routing and Navigation
Provides powerful and highly customizable methods to include parameters into a route. ... orElse("") method to retrieve it.
Read more >
Private Route, Public Route, and Restricted Route with React ...
The difference between two types is defined if restricted props. restricted = false meaning that route is public; or else, it is restricted....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found