Changeset 723


Ignore:
Timestamp:
01/20/14 16:38:34 (10 years ago)
Author:
jts21
Message:

Fix off-by-one error with javascript error handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Javascript2.java

    r722 r723  
    8181                Javascript2 js = new Javascript2(frame, followLinks);
    8282                try {
    83             se.eval(js.toString());
    84         } catch (ScriptException e) {
    85                 js.handleError(e.getMessage(), e.getLineNumber());
    86         } catch (RuntimeException e) {
    87                 // there doesn't seem to be a way to safely get the lineNumber on which the error occurred
    88                 // so as a workaround we just parse the exception
    89                 String detail = e.getCause().getStackTrace()[1].toString();
    90                 int lastColon = detail.lastIndexOf(':');
    91                 int lastBracket = detail.lastIndexOf(')');
    92                 int lineNumber;
    93                 if(lastColon == -1 || lastBracket == -1) {
    94                         lineNumber = -1;
    95                 } else {
    96                         lineNumber = Integer.parseInt(detail.substring(lastColon + 1, lastBracket));
    97                 }
    98                 js.handleError(e.getMessage(), lineNumber);
    99         }
     83                try {
     84                se.eval(js.toString());
     85            } catch (ScriptException e) {
     86                        js.handleError(e.getMessage(), e.getLineNumber());
     87            } catch (RuntimeException e) {
     88                // there doesn't seem to be a way to safely get the lineNumber on which the error occurred
     89                // so as a workaround we just parse the exception
     90                if(e.getCause() == null) {
     91                        throw e;
     92                }
     93                String detail = e.getCause().getStackTrace()[1].toString();
     94                int lastColon = detail.lastIndexOf(':');
     95                int lastBracket = detail.lastIndexOf(')');
     96                int lineNumber;
     97                if(lastColon == -1 || lastBracket == -1) {
     98                        lineNumber = -1;
     99                } else {
     100                        lineNumber = Integer.parseInt(detail.substring(lastColon + 1, lastBracket));
     101                }
     102                js.handleError(e.getMessage(), lineNumber);
     103            }
     104                } catch(Exception e) {
     105                        js.handleError(null, -1);
     106                        System.out.println(js.toString());
     107                        e.printStackTrace();
     108                }
    100109        }
    101110       
     
    109118                        this.line = line;
    110119                        this.source = source;
     120                }
     121               
     122                public String toString() {
     123                        return line + ": " + source;
    111124                }
    112125        }
     
    172185                }
    173186                // if for some reason the error is after the end of the code, assume it should be the last line
    174                 if(lineNumber >= this.lines.size()) {
    175                         lineNumber = this.lines.size() - 1;
     187                if(lineNumber > this.lines.size()) {
     188                        lineNumber = this.lines.size();
    176189                }
    177190                CodeLine cl = this.lines.get(lineNumber - 1);
Note: See TracChangeset for help on using the changeset viewer.