ktorio/ktor

404 doesn't throw an exception

Ouverte

#1 437 ouverte le 13 nov. 2019

 (6 commentaires) (0 réaction) (1 personne assignée)Kotlin (1 254 forks)batch import
bughelp wanted

Métriques du dépôt

Stars
 (14 408 étoiles)
Métriques de merge PR
 (Merge moyen 24j 16h) (58 PRs mergées en 30 j)

Description

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

Guide contributeur