/** * Copyright 2009 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS-IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import("ejs.EJS"); import("funhtml.*"); import("jsutils.{scalaF0,scalaF1}"); import("stringutils.{toHTML,sprintf}"); function _getException(ex) { if (ex instanceof java.lang.Throwable) { return new net.appjet.bodylock.JSRuntimeException(ex.getMessage(), ex); } else if (ex.javaException) { return new net.appjet.bodylock.JSRuntimeException(ex.javaException.getMessage(), ex.javaException); } else if (ex.rhinoException) { return new net.appjet.bodylock.JSRuntimeException(ex.rhinoException.getMessage(), ex.rhinoException); } else { return ex; } } function _convertStackFrameToTable(id, frame) { var r = frame.errorContext(4); var out = []; var t = TABLE({className: "codecontext"}); var counter = r._1(); r._3().foreach(scalaF1(function(s) { var row = TR(TD({className: "linecell"}, counter++), TD(String(s))); if (counter-1 == frame.errorLine()) row[1].attribs['class'] = "offendingline"; t.push(row); })); if (id != 0) out.push(DIV({className: "errorframe", onclick: "toggleFrameView('"+id+"')"}, IMG({className: "zippy", style: "margin-right: 0.5em;", align: "top", src: "http://appjet.com/img/open-arrow.png", id: "image"+id}), SPAN({className: "errordetail"}, "...was called from "+frame.name()+ " (line "+frame.errorLine()+"):"), SPAN({className: "sourceline"}, " "+frame.errorContext(0)._3().first()))); out.push(DIV({id: 'frame'+id, style: (id == 0 ? "" : "display: none;")}, t)); return out.map(function(tag) { return toHTML(tag); }).join(""); } function getStackTraceHTML(ex) { ex = _getException(ex); if (ex.frames().isEmpty()) return "No stack trace available."; var out = []; var counter = 0; var firstFrame = ex.frames().first(); out.push(toHTML(DIV({id: "errortitle"}, "Error in "+firstFrame.name()))); out.push(toHTML(DIV({id: "errormessage"}, ""+ex.cause().getMessage()+" at "+firstFrame.name()+" (Line "+firstFrame.errorLine()+")"))); ex.frames().foreach(scalaF1(function(frame) { out.push(_convertStackFrameToTable(counter++, frame)); })); return out.join(""); } function getStackTraceFullpage(ex) { var tmpl = new EJS({text: _tmpl}); return tmpl.render({trace: getStackTraceHTML(ex)}); } function getStackTracePlain(ex) { ex = _getException(ex); if (ex.frames().isEmpty()) { var cause = ex.cause(); var sw = new java.io.StringWriter(); cause.printStackTrace(new java.io.PrintWriter(sw)); return sw.toString(); } var out = []; var firstFrame = ex.frames().first(); out.push("Error in "+firstFrame.name()); out.push(""+ex.cause().getMessage()+" at "+firstFrame.name()+" (Line "+firstFrame.errorLine()+")"); var counter = 0; ex.frames().foreach(scalaF1(function(frame) { if (counter++ > 0) { out.push(""); out.push("...was called from "+frame.name()+" (line "+frame.errorLine()+"): "+frame.errorContext(0)._3().first()); } var r = frame.errorContext(4); var c2 = r._1(); r._3().foreach(scalaF1(function(s) { var pre = " "; if (c2 == frame.errorLine()) pre = ">"; out.push(sprintf("%s %4s | %s", pre, ""+c2, s)); c2++; })); })); return out.join("\n"); } /* template follows */ var _tmpl = """ AppJet Error <%= trace %> """