Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 doesn't throw an exception #1437

Open
kuuuurt opened this issue Nov 13, 2019 · 5 comments
Open

404 doesn't throw an exception #1437

kuuuurt opened this issue Nov 13, 2019 · 5 comments
Assignees

Comments

@kuuuurt
Copy link

kuuuurt commented Nov 13, 2019

Ktor 1.2.4 - Client

  • OkHttp
  • JSON
  • Serialization

Describe the bug
It seems that it doesn't catch 404's in an error

To Reproduce

  1. Here's my code:
val httpClientEngine by lazy {
    OkHttp.create {
        if (BuildConfig.DEBUG) {
            addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
        }
    }
}

private val httpClient = HttpClient(httpClientEngine) {
    install(JsonFeature) {
        serializer = KotlinxSerializer(Json.nonstrict)
    }
}

suspend fun myFunction(data: Any) = httpCall {
    httpClient.call("my_invalid_url") {
        contentType(ContentType.Application.Json
        method = HttpMethod.Post
        body = MyRequest(data)
    }
}

And here's the httpCall wrapper:

suspend inline fun <T> httpCall(call: () -> T) = try {
    call()
} catch (exception: ClientRequestException) {
    val errorResponse = exception.response.receive<MyErrorResponse>()
    var message = errorResponse.message

    throw Exception(message)
}

However, 404's just pass through this.

Here's the response from the server:

<-- 404 Not Found http://127.0.0.1/my_invalid_url (420ms)        
Server: nginx/1.15.12                                               
Date: Wed, 13 Nov 2019 07:18:25 GMT                                 
Content-Type: text/html; charset=utf-8                              
Content-Length: 152                                                 
Connection: keep-alive                                              
X-Powered-By: Express                                               
Access-Control-Allow-Origin: *                                      
Content-Security-Policy: default-src 'none'                         
X-Content-Type-Options: nosniff                                     
<!DOCTYPE html>                                                     
<html lang="en">                                                    
<head>                                                              
<meta charset="utf-8">                                              
<title>Error</title>                                                
</head>                                                             
<body>                                                              
<pre>Cannot POST /my_invalid_url</pre>                                
</body>                                                             
</html>                                                             
<-- END HTTP (152-byte body)                                        

I'm thinking I should need to register an XML serializer? But I can't find anything in the documentation. If I'm missing something, please do point me in the right direction.

Many thanks!

Expected behavior
It should catch 404's as an error

@kuuuurt kuuuurt added the bug label Nov 13, 2019
@e5l e5l self-assigned this Nov 18, 2019
@e5l
Copy link
Member

e5l commented Nov 18, 2019

Hi @kuuuurt, thanks for the report. Yep, it should be the exception here.
Btw there is no XML feature out of the box.

Probably we can have an integration with https://github.com/pdvrieze/xmlutil for XML

@kuuuurt
Copy link
Author

kuuuurt commented Nov 18, 2019

Seems like it's just the call making errors pass through. I think there's no need for an XML (at least in my case)

@e5l
Copy link
Member

e5l commented Dec 2, 2019

What type of content do you receive from the call? The client validates a call only in call receiving.

@kuuuurt
Copy link
Author

kuuuurt commented Dec 4, 2019

Some JSON data which I deserialize with Kotlinx serialization. This specific case happened when I had a wrong URL and I'm just getting this

<!DOCTYPE html>                                                     
<html lang="en">                                                    
<head>                                                              
<meta charset="utf-8">                                              
<title>Error</title>                                                
</head>                                                             
<body>                                                              
<pre>Cannot POST /my_invalid_url</pre>                                
</body>                                                             
</html>   

instead of the JSON string but when I have the correct URL and receive this:

{
    code: 200
    data: { ... }
}

It works as expected.

I used post<Unit> instead of call and it doesn't bypass the errors anymore.

@oleg-larshin
Copy link

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants