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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2000-2014 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.logging.client.TextLogFormatter;
  21. import com.google.gwt.user.client.ui.Widget;
  22. import com.vaadin.client.ApplicationConfiguration;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.ui.VNotification;
  25. /**
  26. * Log message handler that shows big red notification for {@link Level#SEVERE}
  27. * messages that have a throwable.
  28. *
  29. * @since 7.1
  30. * @author Vaadin Ltd
  31. */
  32. public class ErrorNotificationHandler extends Handler {
  33. public ErrorNotificationHandler() {
  34. setLevel(Level.SEVERE);
  35. setFormatter(new TextLogFormatter(true) {
  36. @Override
  37. protected String getRecordInfo(LogRecord event, String newline) {
  38. return "";
  39. }
  40. });
  41. }
  42. @Override
  43. public void publish(LogRecord record) {
  44. if (!isLoggable(record) || record.getThrown() == null) {
  45. return;
  46. }
  47. try {
  48. String exceptionText = getFormatter().format(record);
  49. Widget owner = null;
  50. if (!ApplicationConfiguration.getRunningApplications().isEmpty()) {
  51. /*
  52. * Make a wild guess and use the first available
  53. * ApplicationConnection. This is better than than leaving the
  54. * exception completely unstyled...
  55. */
  56. ApplicationConnection connection = ApplicationConfiguration
  57. .getRunningApplications().get(0);
  58. owner = connection.getUIConnector().getWidget();
  59. }
  60. VNotification
  61. .createNotification(VNotification.DELAY_FOREVER, owner)
  62. .show("<h1>Uncaught client side exception</h1><br />"
  63. + exceptionText, VNotification.CENTERED, "error");
  64. } catch (Exception e2) {
  65. // Just swallow this exception
  66. }
  67. }
  68. @Override
  69. public void close() {
  70. // Nothing to do
  71. }
  72. @Override
  73. public void flush() {
  74. // Nothing todo
  75. }
  76. }