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

fix : Handle PageNotUpdatedException #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfluencePage>) : RuntimeException()
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -354,6 +370,4 @@ fun confluenceClient(
agent = "text2confl"
}
}

return ConfluenceClientImpl(config.server, "${config.server}/rest/api", client)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -57,18 +59,26 @@ class ContentUploader(
space: String,
parentPageId: String
): List<PageUploadResult> {
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()
}
}

Expand Down
Loading