Skip to content

Commit

Permalink
Improved handling of HTTP response errors (#152)
Browse files Browse the repository at this point in the history
* WIP manage timeouts, show ace for manual entry

* i18n for timeout messages

* Fix missing space

* Fix layout issues for ace editor across tabs

* Refactor, rename

* Use core 6.2.2

* Style cleanup

* Adjust presets

* Increase timeout

* Fix select warnings in console

* Better handling of server produced errors in manual entry
  • Loading branch information
dotasek committed Nov 9, 2023
1 parent 1fe5d18 commit 4f9a766
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/model/ValidationResponseException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package model

class ValidationResponseException(val httpStatusCode: Int, message: String) : Exception(message) {
}
3 changes: 2 additions & 1 deletion src/commonMain/resources/static-content/polyglot/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"enter_resources_heading" : "Ressource eintragen",
"manual_entry_place_holder" : "Ressource manuell eintragen...",
"manual_entry_title" : "Code",
"manual_entry_error" : "Bitte geben Sie etwas zum Validieren an.",
"manual_entry_empty_request_error" : "Bitte geben Sie etwas zum Validieren an.",
"manual_entry_cannot_parse_exception" : "Der eingegebene Text kann nicht als gültiges JSON/XML geparst werden.",
"manual_entry_validation_response_exception" : "Error received from validation server: %{httpResponseCode}-German",
"manual_entry_timeout_exception" : "Validation timed out. Please wait 30 seconds and try again. -German",
"validate_button" : "Validieren",
"upload_resources_heading" : "Ressourcen Hochladen",
Expand Down
3 changes: 3 additions & 0 deletions src/commonMain/resources/static-content/polyglot/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"enter_resources_heading" : "Enter Resource",
"manual_entry_place_holder" : "Enter Resource Manually...",
"manual_entry_title" : "Code",
"manual_entry_empty_request_error" : "Please enter something to validate.",
"manual_entry_timeout_exception" : "Validation timed out. Please wait 30 seconds and try again.",
"manual_entry_validation_response_exception" : "Error received from validation server: %{httpResponseCode}",
"manual_entry_error" : "Please enter something to validate.",
"manual_entry_timeout_exception" : "Validation timed out. Please wait 30 seconds and try again.",
"manual_entry_cannot_parse_exception" : "Cannot parse entered text as valid JSON/XML.",
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/resources/static-content/polyglot/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"enter_resources_heading": "Ingrese el Recurso",
"manual_entry_place_holder": "Ingrese el recurso manualmente...",
"manual_entry_title": "Código",
"manual_entry_error": "Por favor ingrese algo para validar.",
"manual_entry_empty_request_error": "Por favor ingrese algo para validar.",
"manual_entry_cannot_parse_exception": "No se pudo analizar el texto ingresado como JSON o XML.",
"manual_entry_validation_response_exception" : "Error received from validation server: %{httpResponseCode} -Spanish",
"manual_entry_timeout_exception" : "Validation timed out. Please wait 30 seconds and try again. -Spanish",
"validate_button": "Validar",
"upload_resources_heading": "Cargar Recursos",
Expand Down
13 changes: 7 additions & 6 deletions src/jsMain/kotlin/api/ValidatorApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import constants.VALIDATOR_VERSION_ENDPOINT

import io.ktor.client.request.*
import io.ktor.http.*
import kotlinext.js.asJsObject
import io.ktor.client.call.*
import io.ktor.client.statement.*
import model.*

import kotlinx.browser.window

suspend fun sendValidationRequest(validationRequest: ValidationRequest): ValidationResponse {
val myMap = js("{" +
"\"txServer\":\"dummyServer\"" +
"}")
println(myMap)
kotlinx.browser.window.asDynamic().gtag("event", "validationEvent", myMap)
return jsonClient.post(urlString = endpoint + VALIDATION_ENDPOINT) {
val response = jsonClient.post(urlString = endpoint + VALIDATION_ENDPOINT) {
contentType(ContentType.Application.Json)
setBody(validationRequest)
}.body()
}
if (response.status != HttpStatusCode.OK) {
throw ValidationResponseException(response.status.value, response.bodyAsText())
}
return response.body()
}

suspend fun sendValidatorVersionRequest() : String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class LanguageSelect(props : LanguageSelectProps) : RComponent<LanguageSelectPro
+props.polyglot.t("language")
}
Select {

attrs {
label = ReactNode("Language")
value = props.selectedLanguage.getLanguageCode().unsafeCast<Nothing?>()
onChange = { event, _ ->
val selectedLanguage = Language.getSelectedLanguage(event.target.value)
if (selectedLanguage != null) {
Expand Down
11 changes: 9 additions & 2 deletions src/jsMain/kotlin/ui/components/options/PresetSelect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ external interface PresetSelectProps : Props {

class PresetSelectState : State {
var snackbarOpen : String? = null
var preset : String = ""
}

class PresetSelect : RComponent<PresetSelectProps, PresetSelectState>() {
Expand Down Expand Up @@ -81,7 +82,8 @@ class PresetSelect : RComponent<PresetSelectProps, PresetSelectState>() {
}
Select {
attrs {
label = ReactNode("preset")
label = ReactNode("Preset")
value = "".unsafeCast<Nothing?>()
onChange = { event, _ ->
val selectedPreset = Preset.getSelectedPreset(event.target.value)
if (selectedPreset != null) {
Expand Down Expand Up @@ -121,7 +123,12 @@ class PresetSelect : RComponent<PresetSelectProps, PresetSelectState>() {
open = state.snackbarOpen != null
message = ReactNode(
props.polyglot.t("preset_notification",
getJS(arrayOf(Pair("selectedPreset", props.polyglot.t(state.snackbarOpen.toString())))))
getJS(arrayOf(Pair("selectedPreset",
if (state.snackbarOpen != null)
props.polyglot.t(state.snackbarOpen.toString())
else
"---"
))))
)
autoHideDuration=6000
onClose = { event, _ -> handleSnackBarClose() }
Expand Down
14 changes: 12 additions & 2 deletions src/jsMain/kotlin/ui/components/tabs/entrytab/ManualEntryTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ui.components.tabs.heading

import ui.components.validation.validationOutcomeContainer
import utils.assembleRequest
import utils.getJS
import utils.isJson
import utils.isXml

Expand Down Expand Up @@ -100,7 +101,7 @@ class ManualEntryTab : RComponent<ManualEntryTabProps, ManualEntryTabState>() {
if (props.currentManuallyEnteredText.isNotEmpty()) {
validateEnteredText(props.currentManuallyEnteredText)
} else {
val newErrorMessage = props.polyglot.t("manual_entry_error")
val newErrorMessage = props.polyglot.t("manual_entry_empty_request_error")
setState {
errorMessage = newErrorMessage
displayingError = true
Expand Down Expand Up @@ -181,7 +182,16 @@ class ManualEntryTab : RComponent<ManualEntryTabProps, ManualEntryTabState>() {
displayingError = true
}
props.toggleValidationInProgress(false)
} catch (e: Exception) {
} catch (e : ValidationResponseException) {
setState {
errorMessage = props.polyglot.t("manual_entry_validation_response_exception",
getJS(arrayOf(Pair("httpResponseCode", e.httpStatusCode)))
)
displayingError = true
}
println("Exception ${e.message}")
}
catch (e: Exception) {
setState {
if (props.currentManuallyEnteredText.contains("Mark is super dorky")) {
ohShitYouDidIt = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ValidationControllerImpl : ValidationController, KoinComponent {
private val validationServiceFactory by inject<ValidationServiceFactory>()

override suspend fun validateRequest(validationRequest: ValidationRequest): ValidationResponse {

return validationServiceFactory.getValidationService().validateSources(validationRequest)
}

Expand Down

0 comments on commit 4f9a766

Please sign in to comment.