From 54ab7ca121e1e8092c29f0d205a2c8359a28ace8 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 6 Dec 2006 16:28:12 +0000 Subject: [PATCH] * Backport StackUtils class from Helma 2. * Implement jsStackTrace option to generate pure JS stack traces in error dumps. --- src/helma/framework/core/Application.java | 9 ++++- src/helma/util/StackUtils.java | 47 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/helma/util/StackUtils.java diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index 3011168d..aebe1f41 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -1366,8 +1366,13 @@ public final class Application implements Runnable { if (eventLog == null) { eventLog = getLogger(eventLogName); } - - eventLog.error(msg, error); + if ("true".equalsIgnoreCase(props.getProperty("jsStackTrace"))) { + Exception jsx = new RuntimeException(error); + StackUtils.setJavaScriptStack(error, jsx); + eventLog.error(msg, jsx); + } else { + eventLog.error(msg, error); + } } /** diff --git a/src/helma/util/StackUtils.java b/src/helma/util/StackUtils.java new file mode 100644 index 00000000..9106af14 --- /dev/null +++ b/src/helma/util/StackUtils.java @@ -0,0 +1,47 @@ +/* + * Copyright 2006 Hannes Wallnoefer + * + * 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. + */ + +package helma.util; + +import java.util.List; +import java.util.ArrayList; + +/** + * Utility class to extract pure JavaScript stack trace from Java exceptions + */ +public class StackUtils { + + /** + * Extract the JavaScript stack trace element from the source exception + * and copy them over to the target exception. + * @param source the source exception + * @param target the target excepiton + */ + public static void setJavaScriptStack(Throwable source, Throwable target) { + List list = new ArrayList(); + StackTraceElement[] stack = source.getStackTrace(); + for (int i = 0; i < stack.length; i++) { + StackTraceElement e = stack[i]; + String name = e.getFileName(); + if (e.getLineNumber() > -1 && + (name.endsWith(".js") || name.endsWith(".hac"))) { + list.add(e); + } + } + target.setStackTrace((StackTraceElement[]) list.toArray(new StackTraceElement[list.size()])); + } + +}