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

Limit error dialog width in case of long error message #441

Merged
merged 1 commit into from Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,15 +13,11 @@ import javax.swing.SwingUtilities
@ExperimentalComposeUiApi
object DefaultWindowExceptionHandlerFactory : WindowExceptionHandlerFactory {
override fun exceptionHandler(window: Window) = WindowExceptionHandler { throwable ->
// invokeLater here to dispatch a blocking operation (showMessageDialog)
// invokeLater here to dispatch a blocking operation
SwingUtilities.invokeLater {
JOptionPane.showMessageDialog(
// if there was an error during window init, we can't use it as a parent,
// otherwise we will have two exceptions in the log
window.takeIf { it.isDisplayable },
throwable.message ?: "Unknown error", "Error",
JOptionPane.ERROR_MESSAGE
)
// if there was an error during window init, we can't use it as a parent,
// otherwise we will have two exceptions in the log
showErrorDialog(window.takeIf { it.isDisplayable }, throwable)
window.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING))
}
throw throwable
Expand All @@ -46,3 +42,15 @@ fun interface WindowExceptionHandlerFactory {
val LocalWindowExceptionHandlerFactory = staticCompositionLocalOf<WindowExceptionHandlerFactory> {
DefaultWindowExceptionHandlerFactory
}

private fun showErrorDialog(parentComponent: Window?, throwable: Throwable) {
val title = "Error"
val message = throwable.message ?: "Unknown error"
val pane = object : JOptionPane(message, ERROR_MESSAGE) {
// Limit width for long messages
override fun getMaxCharactersPerLineCount(): Int = 120
}
val dialog = pane.createDialog(parentComponent, title)
dialog.isVisible = true
dialog.dispose()
}