Skip to content

Commit

Permalink
Added method throwMessage to blocks.debug which outputs a custom error
Browse files Browse the repository at this point in the history
string.
  • Loading branch information
Joscha Rohmann authored and Kanaye committed Sep 10, 2016
1 parent 5dc25a4 commit 8fe09d4
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lib/blocks/jsdebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ blocks.debug = {
blocks.debug.printErrors(method, args, options, errors);
}),

throwMessage: debugFunc(function(errorMessage, method, style) {
if (!errorMessage) return;

var type = typeof style == 'string' ? style : 'Info';

if (!style || typeof style != 'object') {
style = defaultStyles[type.toLowerCase()];
}

var message = blocks.debug.Message();

message.addSpan(type, style).addText(' in ');

// No method => No name and no usage, so we at least print a stack trace
if (!method) {
message.addText('unspecified method - ')
.addText(errorMessage);

message.beginCollapsedGroup();

message.addText('Stack trace:')
.newLine()
.addText(new Error().stack);

message.endGroup();
}

// Print method name, error message and add usage
if (method) {
message.addSpan(method.name + '()', {background: '#EEE'})
.addText(' - ')
.addText(errorMessage);
addMethodReference(message, method, true);
}

message.print();
}),

printErrors: function (method, args, options, errors) {
if (!blocks.debug.enabled) {
return;
Expand Down Expand Up @@ -275,7 +313,7 @@ function addError(message, error, method, paramNames) {
message.addText(', ');
}
for (index = error.expected; index < error.actual; index++) {
message.addSpan(paramNames[index], blocks.extend({}, defaultStyles.error,{'text-decoration': 'line-through'}));
message.addSpan(paramNames[index], blocks.extend({}, defaultStyles.error, {'text-decoration': 'line-through'}));
if (index != error.actual - 1) {
message.addText(', ');
}
Expand Down

0 comments on commit 8fe09d4

Please sign in to comment.