| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://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 |
goog.provide('goog.debugEnhanceErrorTest'); |
| 16 |
goog.setTestOnly('goog.debugEnhanceErrorTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
var THROW_STRING = 1; |
| 22 |
var THROW_NPE = 2; |
| 23 |
var THROW_ERROR = 3; |
| 24 |
var THROW_ENHANCED_ERROR = 4; |
| 25 |
var THROW_ENHANCED_STRING = 5; |
| 26 |
var THROW_OBJECT = 6; |
| 27 |
|
| 28 |
if (typeof debug == 'undefined') { |
| 29 |
function debug(str) { |
| 30 |
if (window.console) window.console.log(str); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
function testEnhanceError() { |
| 35 |
// Tests are like this: |
| 36 |
// [test num, expect something in the stack, expect an extra message] |
| 37 |
var tests = [ |
| 38 |
[THROW_STRING], [THROW_OBJECT], [THROW_NPE], [THROW_ERROR], |
| 39 |
[THROW_ENHANCED_ERROR, 'throwEnhancedError', 'an enhanced error'], |
| 40 |
[THROW_ENHANCED_STRING, 'throwEnhancedString'] |
| 41 |
]; |
| 42 |
for (var i = 0; i < tests.length; ++i) { |
| 43 |
var test = tests[i]; |
| 44 |
var testNum = test[0]; |
| 45 |
var testInStack = test[1]; |
| 46 |
var testExtraMessage = test[2] || null; |
| 47 |
try { |
| 48 |
foo(testNum); |
| 49 |
} catch (e) { |
| 50 |
debug(goog.debug.expose(e)); |
| 51 |
var s = e.stack.split('\n'); |
| 52 |
for (var j = 0; j < s.length; ++j) { |
| 53 |
debug(s[j]); |
| 54 |
} |
| 55 |
// 'baz' is always in the stack |
| 56 |
assertTrue('stack should contain "baz"', e.stack.indexOf('baz') != -1); |
| 57 |
|
| 58 |
if (testInStack) { |
| 59 |
assertTrue( |
| 60 |
'stack should contain "' + testInStack + '"', |
| 61 |
e.stack.indexOf(testInStack) != -1); |
| 62 |
} |
| 63 |
if (testExtraMessage) { |
| 64 |
// 2 messages |
| 65 |
assertTrue( |
| 66 |
'message0 should contain "' + testExtraMessage + '"', |
| 67 |
e.message0.indexOf(testExtraMessage) != -1); |
| 68 |
assertTrue( |
| 69 |
'message1 should contain "message from baz"', |
| 70 |
e.message1.indexOf('message from baz') != -1); |
| 71 |
} else { |
| 72 |
// 1 message |
| 73 |
assertTrue( |
| 74 |
'message0 should contain "message from baz"', |
| 75 |
e.message0.indexOf('message from baz') != -1); |
| 76 |
} |
| 77 |
continue; |
| 78 |
} |
| 79 |
fail('expected to catch an exception'); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
function foo(testNum) { |
| 85 |
bar(testNum); |
| 86 |
} |
| 87 |
|
| 88 |
function bar(testNum) { |
| 89 |
baz(testNum); |
| 90 |
} |
| 91 |
|
| 92 |
function baz(testNum) { |
| 93 |
try { |
| 94 |
switch (testNum) { |
| 95 |
case THROW_STRING: |
| 96 |
throwString(); |
| 97 |
break; |
| 98 |
case THROW_NPE: |
| 99 |
throwNpe(); |
| 100 |
break; |
| 101 |
case THROW_ERROR: |
| 102 |
throwError(); |
| 103 |
break; |
| 104 |
case THROW_ENHANCED_ERROR: |
| 105 |
throwEnhancedError(); |
| 106 |
break; |
| 107 |
case THROW_ENHANCED_STRING: |
| 108 |
throwEnhancedString(); |
| 109 |
break; |
| 110 |
case THROW_OBJECT: |
| 111 |
throwObject(); |
| 112 |
break; |
| 113 |
} |
| 114 |
} catch (e) { |
| 115 |
throw goog.debug.enhanceError(e, 'message from baz'); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function throwString() { |
| 120 |
throw 'a string'; |
| 121 |
} |
| 122 |
|
| 123 |
function throwNpe() { |
| 124 |
var nada = null; |
| 125 |
nada.noSuchFunction(); |
| 126 |
} |
| 127 |
|
| 128 |
function throwError() { |
| 129 |
throw new Error('an error'); |
| 130 |
} |
| 131 |
|
| 132 |
function throwEnhancedError() { |
| 133 |
throw goog.debug.enhanceError(Error('dang!'), 'an enhanced error'); |
| 134 |
} |
| 135 |
|
| 136 |
function throwEnhancedString() { |
| 137 |
throw goog.debug.enhanceError('oh nos!'); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @throws {*} |
| 142 |
*/ |
| 143 |
function throwObject() { |
| 144 |
throw {property: 'value'}; |
| 145 |
} |