addon-sdk/source/lib/sdk/console/traceback.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 const { Cc, Ci, components } = require("chrome");
michael@0 12 const { parseStack, sourceURI } = require("toolkit/loader");
michael@0 13 const { readURISync } = require("../net/url");
michael@0 14
michael@0 15 exports.sourceURI = sourceURI
michael@0 16
michael@0 17 function safeGetFileLine(path, line) {
michael@0 18 try {
michael@0 19 var scheme = require("../url").URL(path).scheme;
michael@0 20 // TODO: There should be an easier, more accurate way to figure out
michael@0 21 // what's the case here.
michael@0 22 if (!(scheme == "http" || scheme == "https"))
michael@0 23 return readURISync(path).split("\n")[line - 1];
michael@0 24 } catch (e) {}
michael@0 25 return null;
michael@0 26 }
michael@0 27
michael@0 28 function nsIStackFramesToJSON(frame) {
michael@0 29 var stack = [];
michael@0 30
michael@0 31 while (frame) {
michael@0 32 if (frame.filename) {
michael@0 33 stack.unshift({
michael@0 34 fileName: sourceURI(frame.filename),
michael@0 35 lineNumber: frame.lineNumber,
michael@0 36 name: frame.name
michael@0 37 });
michael@0 38 }
michael@0 39 frame = frame.caller;
michael@0 40 }
michael@0 41
michael@0 42 return stack;
michael@0 43 };
michael@0 44
michael@0 45 var fromException = exports.fromException = function fromException(e) {
michael@0 46 if (e instanceof Ci.nsIException)
michael@0 47 return nsIStackFramesToJSON(e.location);
michael@0 48 if (e.stack && e.stack.length)
michael@0 49 return parseStack(e.stack);
michael@0 50 if (e.fileName && typeof(e.lineNumber == "number"))
michael@0 51 return [{fileName: sourceURI(e.fileName),
michael@0 52 lineNumber: e.lineNumber,
michael@0 53 name: null}];
michael@0 54 return [];
michael@0 55 };
michael@0 56
michael@0 57 var get = exports.get = function get() {
michael@0 58 return nsIStackFramesToJSON(components.stack.caller);
michael@0 59 };
michael@0 60
michael@0 61 var format = exports.format = function format(tbOrException) {
michael@0 62 if (tbOrException === undefined) {
michael@0 63 tbOrException = get();
michael@0 64 tbOrException.pop();
michael@0 65 }
michael@0 66
michael@0 67 var tb;
michael@0 68 if (typeof(tbOrException) == "object" &&
michael@0 69 tbOrException.constructor.name == "Array")
michael@0 70 tb = tbOrException;
michael@0 71 else
michael@0 72 tb = fromException(tbOrException);
michael@0 73
michael@0 74 var lines = ["Traceback (most recent call last):"];
michael@0 75
michael@0 76 tb.forEach(
michael@0 77 function(frame) {
michael@0 78 if (!(frame.fileName || frame.lineNumber || frame.name))
michael@0 79 return;
michael@0 80
michael@0 81 lines.push(' File "' + frame.fileName + '", line ' +
michael@0 82 frame.lineNumber + ', in ' + frame.name);
michael@0 83 var sourceLine = safeGetFileLine(frame.fileName, frame.lineNumber);
michael@0 84 if (sourceLine)
michael@0 85 lines.push(' ' + sourceLine.trim());
michael@0 86 });
michael@0 87
michael@0 88 return lines.join("\n");
michael@0 89 };

mercurial