View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import java.io.Serializable;
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.htmlunit.ScriptException;
24  import org.htmlunit.html.HtmlPage;
25  
26  /**
27   * Default implementation of {@link JavaScriptErrorListener} that does
28   * default logging.
29   *
30   * @author Ronald Brill
31   * @author Sven Strickroth
32   */
33  public class DefaultJavaScriptErrorListener implements JavaScriptErrorListener, Serializable {
34  
35      private static final Log LOG = LogFactory.getLog(DefaultJavaScriptErrorListener.class);
36  
37      /**
38       * {@inheritDoc}
39       */
40      @Override
41      public void scriptException(final HtmlPage page, final ScriptException scriptException) {
42          LOG.error("Error during JavaScript execution", scriptException);
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void timeoutError(final HtmlPage page, final long allowedTime, final long executionTime) {
50          if (LOG.isErrorEnabled()) {
51              LOG.error("Timeout during JavaScript execution after "
52                          + executionTime + "ms; allowed only " + allowedTime + "ms");
53          }
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public void malformedScriptURL(final HtmlPage page, final String url,
61                  final MalformedURLException malformedURLException) {
62          if (LOG.isErrorEnabled()) {
63              LOG.error("Unable to build URL for script src tag [" + url + "]", malformedURLException);
64          }
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void loadScriptError(final HtmlPage page, final URL scriptUrl, final Exception exception) {
72          if (LOG.isErrorEnabled()) {
73              LOG.error("Error loading JavaScript from [" + scriptUrl + "].", exception);
74          }
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void warn(final String message, final String sourceName,
82              final int line, final String lineSource, final int lineOffset) {
83          if (LOG.isWarnEnabled()) {
84              final StringBuilder msg = new StringBuilder()
85                      .append("warning: message=[").append(message)
86                      .append("] sourceName=[").append(sourceName)
87                      .append("] line=[").append(line)
88                      .append("] lineSource=[").append(lineSource)
89                      .append("] lineOffset=[").append(lineOffset)
90                      .append(']');
91  
92              LOG.warn(msg.toString());
93          }
94      }
95  }