From 819f30be4e22e64aece6ddced06bc6a8bc4decf7 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 4 Oct 2023 10:12:45 +0200 Subject: [PATCH 1/2] fix : Handle PageNotUpdatedException --- .../zeldigas/confclient/ConfluenceClient.kt | 3 +++ .../confclient/ConfluenceClientImpl.kt | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClient.kt b/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClient.kt index 615a163b..a453cad3 100644 --- a/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClient.kt +++ b/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClient.kt @@ -79,6 +79,9 @@ interface ConfluenceClient { class PageNotCreatedException(val title: String, val status: Int, val body: String?) : RuntimeException("Failed to create '$title' page: status=$status, body:\n$body") +class PageNotUpdatedException(val id: String, val status: Int, val body: String?) : + RuntimeException("Failed to update '$id' page: status=$status, body:\n$body") + class PageNotFoundException : RuntimeException() class TooManyPagesFound(val pages: List) : RuntimeException() \ No newline at end of file diff --git a/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClientImpl.kt b/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClientImpl.kt index 0a81ad59..3bb9a874 100644 --- a/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClientImpl.kt +++ b/confluence-client/src/main/kotlin/com/github/zeldigas/confclient/ConfluenceClientImpl.kt @@ -10,6 +10,7 @@ import com.github.zeldigas.confclient.model.Space import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.engine.cio.* +import io.ktor.client.network.sockets.* import io.ktor.client.plugins.* import io.ktor.client.plugins.auth.* import io.ktor.client.plugins.contentnegotiation.* @@ -133,6 +134,10 @@ class ConfluenceClientImpl( response.body() } catch (e: ContentConvertException) { throw PageNotCreatedException(value.title, response.status.value, response.bodyAsText()) + } catch (e: ConnectTimeoutException) { + throw PageNotCreatedException(value.title, response.status.value, response.bodyAsText()) + } catch (e: HttpRequestTimeoutException) { + throw PageNotCreatedException(value.title, response.status.value, response.bodyAsText()) } } @@ -166,9 +171,13 @@ class ConfluenceClientImpl( setBody(body) } if (response.status.isSuccess()) { - return response.body() + return try { + response.body() + } catch (e: ConnectTimeoutException) { + throw PageNotUpdatedException(pageId, response.status.value, response.bodyAsText()) + } } else { - throw RuntimeException("Failed to update $pageId: ${response.bodyAsText()}") + throw PageNotUpdatedException(pageId, response.status.value, response.bodyAsText()) } } @@ -326,7 +335,14 @@ private data class PageSearchResult( fun confluenceClient( config: ConfluenceClientConfig ): ConfluenceClient { - val client = HttpClient(CIO) { + val client = httpClient(config) + return ConfluenceClientImpl(config.server, "${config.server}/rest/api", client) +} + +private fun httpClient( + config: ConfluenceClientConfig +): HttpClient{ + return HttpClient(CIO) { if (config.skipSsl) { engine { https { @@ -354,6 +370,4 @@ fun confluenceClient( agent = "text2confl" } } - - return ConfluenceClientImpl(config.server, "${config.server}/rest/api", client) } \ No newline at end of file From fe993d6bf86eb5276a29d9005408b4320d7df46c Mon Sep 17 00:00:00 2001 From: David Date: Wed, 4 Oct 2023 10:18:01 +0200 Subject: [PATCH 2/2] fix : catch page creation failures --- .../text2confl/core/upload/ContentUploader.kt | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/com/github/zeldigas/text2confl/core/upload/ContentUploader.kt b/core/src/main/kotlin/com/github/zeldigas/text2confl/core/upload/ContentUploader.kt index ad2527a5..3df61d7a 100644 --- a/core/src/main/kotlin/com/github/zeldigas/text2confl/core/upload/ContentUploader.kt +++ b/core/src/main/kotlin/com/github/zeldigas/text2confl/core/upload/ContentUploader.kt @@ -1,6 +1,8 @@ package com.github.zeldigas.text2confl.core.upload import com.github.zeldigas.confclient.ConfluenceClient +import com.github.zeldigas.confclient.PageNotCreatedException +import com.github.zeldigas.confclient.PageNotUpdatedException import com.github.zeldigas.confclient.model.ConfluencePage import com.github.zeldigas.text2confl.convert.EditorVersion import com.github.zeldigas.text2confl.convert.Page @@ -57,18 +59,26 @@ class ContentUploader( space: String, parentPageId: String ): List { - return coroutineScope { - pages.map { page -> - async { - val result = uploadPage(page, space, parentPageId) - buildList { - add(result) - if (page.children.isNotEmpty()) { - addAll(uploadPagesRecursive(page.children, space, result.page.id)) + return try { + coroutineScope { + pages.map { page -> + async { + val result = uploadPage(page, space, parentPageId) + buildList { + add(result) + if (page.children.isNotEmpty()) { + addAll(uploadPagesRecursive(page.children, space, result.page.id)) + } } } - } - }.awaitAll().flatten() + }.awaitAll().flatten() + } + } catch (e: PageNotCreatedException){ + logger.error { e.message } + return emptyList() + } catch (e: PageNotUpdatedException){ + logger.error { e.message } + return emptyList() } }