<commit_before>/* eslint-disable max-params, complexity, max-statements */

import { inspect } from 'util';

import merge from './merge';

// Public API
// =========

/*
`notate` is a new alternative to the these two methods of dealing with errors in
javascript:

```
if (err) {
  return cb(err);
}
```

and

```
if (err) {
  myFavoriteLogger.error('methodName: strange situation! ' + JSON.stringify(user));
  return cb(err);
}
```

To accomplish the same thing with `notate()`, attach the user to the error for
future log output, and `add()` adds the current file and line to the top of the stack.
So after an error happens deep in some library, you know how it propagated through your
code afterwards.

```
if (notate(cb, err, {user: user})) {
  return;
}
```

The first parameter, `err` is required. If `err` is truthy, any provided callback will
be called and `add()` will return true.

There are three more optional parameters:

+ `cb` is called with `err` if `err` is truthy.
+ `data` will have its keys merged into the returned error
+ `depth` allows you to put this method in your own helper, just set the depth to the
number of stack frames you add between the client code and `notate()`.
*/
export default function notate(cb, err, data, providedDepth) {
  if (cb && !_isFunction(cb)) {
    throw new Error('notate: provided first parameter cb was not a function');
  }
  if (!err) {
    return false;
  }

  const depth = (providedDepth || 0) + _internals.layerSize;
  justNotate(err, data, depth);

  if (cb) {
    // we really mean to omit the return here
    /* eslint-disable callback-return */
    cb(err);
    /* eslint-enable callback-return */
  }

  return true;
}

/*
`justNotate` does the same thing as `notate`, without taking (and requiring) the initial
`cb` parameter.
*/
export function justNotate(err, data, providedDepth) {
  if (!err) {
    return false;
  }

  try {
    const depth = (providedDepth || 0) + _internals.layerSize;

    const stack = _getStackTrace(depth);
    const line = _getFirstLine(stack);

    _insert(err, line);
    merge(err, data);
  }
  catch (e) {
    // do nothing; we must never crash!
  }

  return true;
}


/*
`prettyPrint()  ` prints out errors. Now that you're using `breadcrumbs.add()` to annotate
your errors as they propagate through your system, it's time to print them out properly.

First we do a `util.inspect()` of the error, grabbing all of those keys we've merged
into it for debugging purposes, and then we print out the stack after removing all
instances of `process.cwd()` (if we're on the server).

You can prevent the stack from being printed by setting `err.log` to something other
than 'warn' or 'error'.
*/
export function prettyPrint(err, providedOptions) {
  let result = '';

  try {
    if (!err) {
      return result;
    }
    if (!_isError(err)) {
      return inspect(err);
    }<commit_msg>docs(prettyPrint): fix formatting, remove breadcrumbs reference<commit_after>/* eslint-disable max-params, complexity, max-statements */

import { inspect } from 'util';

import merge from './merge';

// Public API
// =========

/*
`notate` is a new alternative to the these two methods of dealing with errors in
javascript:

```
if (err) {
  return cb(err);
}
```

and

```
if (err) {
  myFavoriteLogger.error('methodName: strange situation! ' + JSON.stringify(user));
  return cb(err);
}
```

To accomplish the same thing with `notate()`, attach the user to the error for
future log output, and `add()` adds the current file and line to the top of the stack.
So after an error happens deep in some library, you know how it propagated through your
code afterwards.

```
if (notate(cb, err, {user: user})) {
  return;
}
```

The first parameter, `err` is required. If `err` is truthy, any provided callback will
be called and `add()` will return true.

There are three more optional parameters:

+ `cb` is called with `err` if `err` is truthy.
+ `data` will have its keys merged into the returned error
+ `depth` allows you to put this method in your own helper, just set the depth to the
number of stack frames you add between the client code and `notate()`.
*/
export default function notate(cb, err, data, providedDepth) {
  if (cb && !_isFunction(cb)) {
    throw new Error('notate: provided first parameter cb was not a function');
  }
  if (!err) {
    return false;
  }

  const depth = (providedDepth || 0) + _internals.layerSize;
  justNotate(err, data, depth);

  if (cb) {
    // we really mean to omit the return here
    /* eslint-disable callback-return */
    cb(err);
    /* eslint-enable callback-return */
  }

  return true;
}

/*
`justNotate` does the same thing as `notate`, without taking (and requiring) the initial
`cb` parameter.
*/
export function justNotate(err, data, providedDepth) {
  if (!err) {
    return false;
  }

  try {
    const depth = (providedDepth || 0) + _internals.layerSize;

    const stack = _getStackTrace(depth);
    const line = _getFirstLine(stack);

    _insert(err, line);
    merge(err, data);
  }
  catch (e) {
    // do nothing; we must never crash!
  }

  return true;
}


/*
`prettyPrint()` prints out errors. Now that you're using `notate()` to annotate
your errors as they propagate through your system, it's time to print them out properly.

First we do a `util.inspect()` of the error, grabbing all of those keys we've merged
into it for debugging purposes, and then we print out the stack after removing all
instances of `process.cwd()` (if we're on the server).

You can prevent the stack from being printed by setting `err.log` to something other
than 'warn' or 'error'.
*/
export function prettyPrint(err, providedOptions) {
  let result = '';

  try {
    if (!err) {
      return result;
    }
    if (!_isError(err)) {
      return inspect(err);
    }
{'fraction_non_alphanumeric': 0.11631914526968809, 'fraction_numerical': 0.0006892986386351886, 'mean_word_length': 3.5450274079874706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '53433', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1730, 'n_words': 747}