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.

Override exceptions from the http client with a feature?

See original GitHub issue

I haven’t been able to find a way to override the BadResponseStatusException with a ktor feature. My team has previously overridden exceptions to a common format so that they are easier to handle. We also don’t like to leak the HttpClient code into the entire application, but instead like to create a service module, where it’s completely encapsulated from the rest of the application.

We’ve tried a number of ways to override the exception using features, but nothing has quite worked as we’ve expected. If this is currently possible, can you please indicate how this can be done, otherwise please add this as a feature request.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ScottPiercecommented, Jan 17, 2019

@e5l Doesn’t work. If we look at https://github.com/ktorio/ktor/blob/master/ktor-client/ktor-client-core/common/src/io/ktor/client/call/HttpClientCall.kt#L54-L62 the problem is that all exceptions that aren’t BadResponseStatusException are overridden.

I added:

HttpClient(ServicePlatform.httpClientEngineFactory) {
      expectSuccess = false
}

And also:

scope.responsePipeline.intercept(HttpResponsePipeline.Receive) {
  val response = subject.response as HttpResponse
  if (!response.status.isSuccess()) throw RuntimeException("Http Status: ${response.status.value}")
  proceedWith(subject)
}

My result is:

io.ktor.client.call.ReceivePipelineException: Fail to run receive pipeline
    at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:61)
    at io.ktor.client.call.HttpClientCall$receive$1.invokeSuspend(Unknown Source:12)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(PipelineContext.kt:183)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:160)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:111)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:131)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:24)
    at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:55)
    at com.example.api.service.AuthServiceImpl.getNonce(AuthService.kt:120)
    at com.example.api.service.AuthServiceImpl$getNonce$1.invokeSuspend(Unknown Source:32)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(PipelineContext.kt:183)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:142)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(PipelineContext.kt:92)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:45)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(PipelineContext.kt:183)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:142)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(PipelineContext.kt:92)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:45)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(PipelineContext.kt:183)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:142)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(PipelineContext.kt:92)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:45)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.RuntimeException: Http Status: 400
    at com.example.api.service.feature.ExampleFeature$Feature$install$1.invokeSuspend(RallyFeature.kt:38)
    at com.example.api.service.feature.ExampleFeature$Feature$install$1.invoke(Unknown Source:40)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:248)
	... 27 more
0reactions
e5lcommented, Mar 19, 2019

The fix in master. See HttpResponseValidator feature for the details.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HttpClient exception handling guide
HttpException represents an abstract logical error in HttpClient. Generally this kind of exception cannot be automatically recovered from. org.apache.commons.
Read more >
How to override a method when using httpclient? What else is ...
We are using httpclient to connect with a website where there are a lot of redirects. After we tested our initial implementation, ...
Read more >
Handling Exceptions Using the Angular HttpClient Service
We will discuss some of the best ways to manage HTTP exceptions within your app while using Angular's HttpClient service.
Read more >
Exception Handling in ASP.NET Web API - Microsoft Learn
Describes ASP.NET Web API executes error and exception handling and provides examples for errors and exceptions.
Read more >
Handling Errors - FastAPI
When a request contains invalid data, FastAPI internally raises a RequestValidationError . And it also includes a default exception handler for it. To...
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