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

Custom message not shown on Production mode #81

Open
FedericoQuarin opened this issue Jun 11, 2024 · 3 comments
Open

Custom message not shown on Production mode #81

FedericoQuarin opened this issue Jun 11, 2024 · 3 comments
Assignees

Comments

@FedericoQuarin
Copy link
Member

Describe the bug

On production mode, the error window is not showing the custom message that was set, just the error code.
errorWindow

Expected behavior

No response

Minimal reproducible example

On the demo, setting production mode and clicking Throw Error with custom message

Add-on Version

4.0.1

Vaadin Version

24

Additional information

No response

@javier-godoy
Copy link
Member

In worked that way until 3.7.0

protected Html createErrorLabel() {
String label = errorMessage == null ? i18n.getDefaultErrorMessage() : errorMessage;
if (productionMode) {
label =
label.concat(
String.format(
"<br />%s<br /><span class='uuid'>%s</span>",
i18n.getInstructions(), uuid));
}
final Html errorLabel = new Html("<span>" + label + "</span>");
errorLabel.getElement().getClassList().add("errorlabel");
return errorLabel;
}

In version 4.0.0 the method was refactored as

protected Component createErrorLabel() {
Div errorLabel = new Div();
errorLabel.setClassName("errorlabel");
if (productionMode) {
Div instructions = new Div(new Text(i18n.getInstructions()));
Span uuidSpan = new Span(uuid);
uuidSpan.setClassName("uuid");
errorLabel.add(instructions, uuidSpan);
} else {
String label = errorMessage == null ? i18n.getDefaultErrorMessage() : errorMessage;
errorLabel.setText(label);
}

@javier-godoy
Copy link
Member

javier-godoy commented Jun 11, 2024

The default error handler initializes errorMessage from the throwable.getLocalizedMessage() (which, regardless of its name, is almost never localized). For unexpected exceptions, it does not make sense to display that information to final users.

You can configure a custom ErrorWindowFactory for a given exception class (and initialize the error window there as you see fit)

public static void setErrorWindowFactory(Class<? extends Throwable> clazz,
ErrorWindowFactory errorWindowFactory) {
factories.put(clazz.asSubclass(Throwable.class), errorWindowFactory);
}

Alternatively, you can provide custom "instructions" and open ErrorWindow explicitly:

Button throwErrorWithCustomMessageAndCustomTexts =
new Button(
"Throw Error with custom message (custom labels)",
ev -> {
try {
Integer.parseInt("asdf");
} catch (NumberFormatException e) {
ErrorWindowI18n i18n = ErrorWindowI18n.createDefault();
i18n.setCaption("Uh oh... that's embarrassing");
i18n.setInstructions(
"Please pass this unique error identifier to your administrator");
i18n.setClose("Ok");
i18n.setDetails("Show me technical details");
i18n.setDefaultErrorMessage("Something really strange happened");
ErrorWindow w = new ErrorWindow(e, "CUSTOM ERROR MESSAGE", i18n);
w.open();
}
});

@javier-godoy
Copy link
Member

The constructor (Throwable, String) seems to suggest that the string will be displayed as "instructions" (in production mode) and that it will override the exception message (in development mode). As explained above, this is not the current intention of the API, but we acknowledge that it's not good DX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To Do
Development

No branches or pull requests

2 participants