Skip to content

Commit

Permalink
FLUID-5475: Improve debugging support on node further - make our JSON…
Browse files Browse the repository at this point in the history
… logger resistant to objects with broken synthetic properties of the kind that arise from express
  • Loading branch information
amb26 committed Aug 15, 2014
1 parent 81b0fdd commit 448be73
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/framework/core/js/FluidDebugging.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,24 @@ var fluid = fluid || fluid_2_0;
}
return togo;
};

// Marker so that we can render a custom string for properties which are not direct and concrete
fluid.SYNTHETIC_PROPERTY = {};

// utility to avoid triggering custom getter code which may have been written by an incompetent person who throws an exception
fluid.getSafeProperty = function (obj, key) {
var desc = Object.getOwnPropertyDescriptor(obj, key); // supported on all of our environments - is broken on IE8
return desc && !desc.get ? obj[key] : fluid.SYNTHETIC_PROPERTY;
};

function printImpl (obj, small, options) {
var big = small + options.indentChars, togo, isFunction = typeof(obj) === "function";
if (obj === null) {
togo = "null";
} else if (obj === undefined) {
togo = "undefined"; // NB - object invalid for JSON interchange
} else if (obj === fluid.SYNTHETIC_PROPERTY) {
togo = "[Synthetic property]";
} else if (fluid.isPrimitive(obj) && !isFunction) {
togo = JSON.stringify(obj);
}
Expand All @@ -186,9 +197,10 @@ var fluid = fluid || fluid_2_0;
else {
i = 0;
togo = "{" + (isFunction ? " Function" : "") + "\n"; // NB - Function object invalid for JSON interchange
fluid.each(obj, function (value, key) {
for (var key in obj) {
var value = fluid.getSafeProperty(obj, key);
j[i++] = JSON.stringify(key) + ": " + printImpl(value, big, options);
});
}
togo += big + j.join(",\n" + big) + "\n" + small + "}";
}
options.stack.pop();
Expand Down
1 change: 1 addition & 0 deletions tests/framework-tests/core/html/FluidDebugging-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<!-- These are the required javascript modules -->
<script type="text/javascript" src="../../../../src/lib/jquery/core/js/jquery.js"></script>
<script type="text/javascript" src="../../../../src/framework/core/js/FluidDocument.js"></script>
<script type="text/javascript" src="../../../../src/framework/core/js/Fluid.js"></script>
<script type="text/javascript" src="../../../../src/framework/core/js/FluidDebugging.js"></script>

Expand Down
8 changes: 8 additions & 0 deletions tests/framework-tests/core/js/FluidDebuggingTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
var renderedComplex = fluid.prettyPrintJSON(complex);
var reparsed = JSON.parse(renderedComplex);
jqUnit.assertDeepEq("Round-tripping complex object", complex, reparsed);

function Synthetic () {}
var proto = { b: 3 };
Synthetic.prototype = proto;
var synthetic = new Synthetic();

var renderedSynthetic = fluid.prettyPrintJSON(synthetic);
jqUnit.assertTrue("Caught synthetic property", renderedSynthetic.indexOf("[Synthetic property]") !== -1);
});
})();

0 comments on commit 448be73

Please sign in to comment.