Add possibility to disable stacktraces on HTTP 500 error responses

This commit is contained in:
2020-05-16 18:58:54 +02:00
parent 028d16fdc6
commit 6ae7b9632c
5 changed files with 32 additions and 7 deletions

View File

@@ -41,9 +41,15 @@ class WebServer(
throw WebException(BAD_REQUEST, "Unknown error") throw WebException(BAD_REQUEST, "Unknown error")
} catch (e: WebException) { } catch (e: WebException) {
return newFixedLengthResponse(e.status, MIME_JSON, e.json) return handleWebException(e)
} catch (e: Exception) { } catch (e: Exception) {
return newFixedLengthResponse(INTERNAL_ERROR, MIME_PLAINTEXT, e.toString()) return handleUnknownException(e)
}
}
private fun assertThatTTSIsReady() {
if (tts.status != TTSStatus.READY) {
throw WebException(NOT_ACCEPTABLE, "Server is not ready yet")
} }
} }
@@ -57,10 +63,16 @@ class WebServer(
} }
} }
private fun assertThatTTSIsReady() { private fun handleWebException(e: WebException) =
if (tts.status != TTSStatus.READY) { newFixedLengthResponse(e.status, MIME_JSON, e.json)
throw WebException(NOT_ACCEPTABLE, "Server is not ready yet")
private fun handleUnknownException(e: Exception): Response {
val stacktrace = when (preferences.getBoolean(PreferenceKey.ENABLE_HTTP_DEBUG, false)) {
true -> e.toString()
false -> ""
} }
return newFixedLengthResponse(INTERNAL_ERROR, MIME_PLAINTEXT, stacktrace)
} }
private fun say(session: IHTTPSession): Response { private fun say(session: IHTTPSession): Response {

View File

@@ -3,6 +3,7 @@ package io.bartek.ttsserver.ui.preference
object PreferenceKey { object PreferenceKey {
const val PORT = "preference_port" const val PORT = "preference_port"
const val ENABLE_HTTP_DEBUG = "preference_http_debug"
const val ENABLE_SAY_ENDPOINT = "preference_enable_say_endpoint" const val ENABLE_SAY_ENDPOINT = "preference_enable_say_endpoint"
const val ENABLE_WAVE_ENDPOINT = "preference_enable_wave_endpoint" const val ENABLE_WAVE_ENDPOINT = "preference_enable_wave_endpoint"
const val ENABLE_SONOS_ENDPOINT = "preference_enable_sonos_endpoint" const val ENABLE_SONOS_ENDPOINT = "preference_enable_sonos_endpoint"

View File

@@ -20,6 +20,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
private lateinit var sayEndpointPreference: SwitchPreference private lateinit var sayEndpointPreference: SwitchPreference
private lateinit var waveEndpointPreference: SwitchPreference private lateinit var waveEndpointPreference: SwitchPreference
private lateinit var sonosEndpointPreference: SwitchPreference private lateinit var sonosEndpointPreference: SwitchPreference
private lateinit var httpDebugPreference: SwitchPreference
private lateinit var ttsEnginePreference: Preference private lateinit var ttsEnginePreference: Preference
private lateinit var clearSonosCachePreference: Preference private lateinit var clearSonosCachePreference: Preference
@@ -55,6 +56,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
setPreferencesFromResource(R.xml.preferences, rootKey) setPreferencesFromResource(R.xml.preferences, rootKey)
portPreference = findPreference(PreferenceKey.PORT)!! portPreference = findPreference(PreferenceKey.PORT)!!
portPreference.setOnBindEditTextListener { it.inputType = InputType.TYPE_CLASS_NUMBER } portPreference.setOnBindEditTextListener { it.inputType = InputType.TYPE_CLASS_NUMBER }
httpDebugPreference = findPreference(PreferenceKey.ENABLE_HTTP_DEBUG)!!
sayEndpointPreference = findPreference(PreferenceKey.ENABLE_SAY_ENDPOINT)!! sayEndpointPreference = findPreference(PreferenceKey.ENABLE_SAY_ENDPOINT)!!
waveEndpointPreference = findPreference(PreferenceKey.ENABLE_WAVE_ENDPOINT)!! waveEndpointPreference = findPreference(PreferenceKey.ENABLE_WAVE_ENDPOINT)!!
sonosEndpointPreference = findPreference(PreferenceKey.ENABLE_SONOS_ENDPOINT)!! sonosEndpointPreference = findPreference(PreferenceKey.ENABLE_SONOS_ENDPOINT)!!

View File

@@ -23,12 +23,15 @@
<string name="preference_enable_wave_endpoint_title">Enable /wave endpoint</string> <string name="preference_enable_wave_endpoint_title">Enable /wave endpoint</string>
<string name="preference_enable_sonos_endpoint_summary">Allow HTTP clients to use /sonos endpoint which enables them to send TTS messages directly to Sonos devices</string> <string name="preference_enable_sonos_endpoint_summary">Allow HTTP clients to use /sonos endpoint which enables them to send TTS messages directly to Sonos devices</string>
<string name="preference_enable_sonos_endpoint_title">Enable /sonos endpoint</string> <string name="preference_enable_sonos_endpoint_title">Enable /sonos endpoint</string>
<string name="preference_http_debug_summary">Attach the stacktrace to each error HTTP response if available</string>
<string name="preference_http_debug_title">Enable HTTP debugging</string>
<string name="preference_tts_summary">Go to platform\'s TTS engine settings and adjust its parameters</string> <string name="preference_tts_summary">Go to platform\'s TTS engine settings and adjust its parameters</string>
<string name="preference_tts_title">TTS engine settings</string> <string name="preference_tts_title">TTS engine settings</string>
<string name="preference_invalidate_sonos_cache_summary">Clear application cache directory from already generated Sonos TTS data for given sentences. You typically want to do it when you change the TTS engine\'s voice settings.</string> <string name="preference_invalidate_sonos_cache_summary">Clear application cache directory from already generated Sonos TTS data for given sentences. You typically want to do it when you change the TTS engine\'s voice settings.</string>
<string name="preference_invalidate_sonos_cache_title">Invalidate Sonos cache</string> <string name="preference_invalidate_sonos_cache_title">Invalidate Sonos cache</string>
<string name="preference_category_tts">TTS engine</string>
<string name="preference_category_server">Server</string> <string name="preference_category_server">Server</string>
<string name="preference_category_features">Features</string>
<string name="preference_category_tts">TTS engine</string>
<string name="preference_category_sonos">Sonos</string> <string name="preference_category_sonos">Sonos</string>
<string name="preference_invalidate_sonos_cache_toast">Sonos cache has been invalidated</string> <string name="preference_invalidate_sonos_cache_toast">Sonos cache has been invalidated</string>

View File

@@ -9,7 +9,15 @@
android:summary="@string/preference_port_summary" android:summary="@string/preference_port_summary"
android:title="@string/preference_port_title" android:title="@string/preference_port_title"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="false"
android:key="preference_http_debug"
android:summary="@string/preference_http_debug_summary"
android:title="@string/preference_http_debug_title"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/preference_category_features" app:iconSpaceReserved="false">
<SwitchPreference <SwitchPreference
android:defaultValue="true" android:defaultValue="true"
android:key="preference_enable_say_endpoint" android:key="preference_enable_say_endpoint"
@@ -30,7 +38,6 @@
android:summary="@string/preference_enable_sonos_endpoint_summary" android:summary="@string/preference_enable_sonos_endpoint_summary"
android:title="@string/preference_enable_sonos_endpoint_title" android:title="@string/preference_enable_sonos_endpoint_title"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/preference_category_tts" app:iconSpaceReserved="false"> <PreferenceCategory android:title="@string/preference_category_tts" app:iconSpaceReserved="false">