aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ava/lib/serialize-error.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-28 00:38:50 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-28 00:40:43 +0200
commit7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch)
tree6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/ava/lib/serialize-error.js
parent963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff)
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/ava/lib/serialize-error.js')
-rw-r--r--node_modules/ava/lib/serialize-error.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/node_modules/ava/lib/serialize-error.js b/node_modules/ava/lib/serialize-error.js
new file mode 100644
index 000000000..55717e161
--- /dev/null
+++ b/node_modules/ava/lib/serialize-error.js
@@ -0,0 +1,94 @@
+'use strict';
+const path = require('path');
+const cleanYamlObject = require('clean-yaml-object');
+const StackUtils = require('stack-utils');
+const assert = require('./assert');
+const beautifyStack = require('./beautify-stack');
+const extractStack = require('./extract-stack');
+
+function isAvaAssertionError(source) {
+ return source instanceof assert.AssertionError;
+}
+
+function filter(propertyName, isRoot) {
+ return !isRoot || (propertyName !== 'message' && propertyName !== 'name' && propertyName !== 'stack');
+}
+
+const stackUtils = new StackUtils();
+function extractSource(stack) {
+ if (!stack) {
+ return null;
+ }
+
+ const firstStackLine = extractStack(stack).split('\n')[0];
+ return stackUtils.parseLine(firstStackLine);
+}
+function buildSource(source) {
+ if (!source) {
+ return null;
+ }
+
+ // Assume the CWD is the project directory. This holds since this function
+ // is only called in test workers, which are created with their working
+ // directory set to the project directory.
+ const projectDir = process.cwd();
+
+ const file = path.resolve(projectDir, source.file.trim());
+ const rel = path.relative(projectDir, file);
+
+ const isWithinProject = rel.split(path.sep)[0] !== '..';
+ const isDependency = isWithinProject && path.dirname(rel).split(path.sep).indexOf('node_modules') > -1;
+
+ return {
+ isDependency,
+ isWithinProject,
+ file,
+ line: source.line
+ };
+}
+
+module.exports = error => {
+ const stack = typeof error.stack === 'string' ?
+ beautifyStack(error.stack) :
+ null;
+
+ const retval = {
+ avaAssertionError: isAvaAssertionError(error),
+ source: buildSource(extractSource(stack))
+ };
+ if (stack) {
+ retval.stack = stack;
+ }
+
+ if (retval.avaAssertionError) {
+ retval.improperUsage = error.improperUsage;
+ retval.message = error.message;
+ retval.name = error.name;
+ retval.statements = error.statements;
+ retval.values = error.values;
+
+ if (error.fixedSource) {
+ const source = buildSource(error.fixedSource);
+ if (source) {
+ retval.source = source;
+ }
+ }
+
+ if (error.assertion) {
+ retval.assertion = error.assertion;
+ }
+ if (error.operator) {
+ retval.operator = error.operator;
+ }
+ } else {
+ retval.object = cleanYamlObject(error, filter); // Cleanly copy non-standard properties
+ if (typeof error.message === 'string') {
+ retval.message = error.message;
+ }
+ if (typeof error.name === 'string') {
+ retval.name = error.name;
+ }
+ }
+
+ return retval;
+};