Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[fixed] logger.logError is now truly cross platform and cross brows…
Browse files Browse the repository at this point in the history
…er, with a cleaner stack trace
  • Loading branch information
thealjey committed Jan 29, 2017
1 parent 42d1413 commit ebe1b12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions interfaces/clean-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* @flow */

declare module 'clean-stack' {
declare function exports(stack: string): string;
}
12 changes: 12 additions & 0 deletions interfaces/error-stack-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* @flow */

declare module 'error-stack-parser' {
declare class exports {
static parse(error: Error): {
functionName?: string;
fileName: string;
lineNumber: number;
columnNumber: number;
};
}
}
27 changes: 9 additions & 18 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* @flow */

import type {LintError, ConsoleStyleConfig, PostCSSWarning, NodeSassError} from './typedef';
import {sep} from 'path';
import forEach from 'lodash/forEach';
import map from 'lodash/map';
import transform from 'lodash/transform';
import ErrorStackParser from 'error-stack-parser';
import cleanStack from 'clean-stack';

/* eslint-disable no-console */

const pattern = /at (?:(.*) )?\(?([^(]*):(\d+):(\d+)\)?$/,
separatorPattern = new RegExp(sep.replace('\\', '\\\\'));

/**
* Dead-simple, composable, isomorphic, cross-browser wrapper for `console.log`.
*
Expand Down Expand Up @@ -323,23 +321,16 @@ export function log(...messages: Array<string | number | Message>) {
*
* logError(new Error('Some error message'));
*/
export function logError({message, stack}: Error) {
const lines = stack.split('\n'),
{length} = lines;

log(formatErrorMarker(), ': ', message);
export function logError(error: Error) {
const {name, message, stack} = error;

for (let i = 1; i < length; ++i) {
const parts = lines[i].match(pattern);
log(formatErrorMarker(name), ': ', message);

if (parts) {
const [, method, file, line, column] = parts;
error.stack = cleanStack(stack);

if (separatorPattern.test(file)) {
log(' • ', ...formatLine(method || 'unknown', file, line, column));
}
}
}
forEach(ErrorStackParser.parse(error), ({functionName = 'unknown', fileName, lineNumber, columnNumber}) => {
log(' • ', ...formatLine(functionName, fileName, lineNumber, columnNumber));
});
}

/**
Expand Down

0 comments on commit ebe1b12

Please sign in to comment.