You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorNotificationHandler.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.debug.internal;
  17. import java.util.logging.Handler;
  18. import java.util.logging.Level;
  19. import java.util.logging.LogRecord;
  20. import com.google.gwt.dom.client.Style.TextAlign;
  21. import com.google.gwt.logging.client.TextLogFormatter;
  22. import com.google.gwt.user.client.ui.Widget;
  23. import com.vaadin.client.ApplicationConfiguration;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.ui.VNotification;
  26. /**
  27. * Log message handler that shows big red notification for {@link Level#SEVERE}
  28. * messages that have a throwable.
  29. *
  30. * @since 7.1
  31. * @author Vaadin Ltd
  32. */
  33. public class ErrorNotificationHandler extends Handler {
  34. public ErrorNotificationHandler() {
  35. setLevel(Level.SEVERE);
  36. setFormatter(new TextLogFormatter(true) {
  37. @Override
  38. protected String getRecordInfo(LogRecord event, String newline) {
  39. return "";
  40. }
  41. });
  42. }
  43. @Override
  44. public void publish(LogRecord record) {
  45. if (!isLoggable(record) || record.getThrown() == null) {
  46. return;
  47. }
  48. try {
  49. String exceptionText = getFormatter().format(record);
  50. Widget owner = null;
  51. if (!ApplicationConfiguration.getRunningApplications().isEmpty()) {
  52. /*
  53. * Make a wild guess and use the first available
  54. * ApplicationConnection. This is better than than leaving the
  55. * exception completely unstyled...
  56. */
  57. ApplicationConnection connection = ApplicationConfiguration
  58. .getRunningApplications().get(0);
  59. owner = connection.getUIConnector().getWidget();
  60. }
  61. VNotification n = VNotification
  62. .createNotification(VNotification.DELAY_FOREVER, owner);
  63. n.getElement().getStyle().setTextAlign(TextAlign.LEFT);
  64. n.show("<h1>Uncaught client side exception</h1><br />"
  65. + exceptionText.replace("\n", "<br/>\n"),
  66. VNotification.CENTERED, "error");
  67. } catch (Exception e2) {
  68. // Just swallow this exception
  69. }
  70. }
  71. @Override
  72. public void close() {
  73. // Nothing to do
  74. }
  75. @Override
  76. public void flush() {
  77. // Nothing todo
  78. }
  79. }