import {pretty, p, pp} from '../universal/base';

function _info(err) {
  return `${pretty(err)}\n  ${
    (err &&
      err.stack &&
      err.stack.replace(/\n/gm, '\n  ')
    ) || 'no stack'
  }`;
}

function _reportError(err, note = 'error') {
  if (window.bundleError &&
    err.sourceURL && err.sourceURL.endsWith('bundle.js')
  ) {
    p(`${_info(err)}`,
      `(${note}) ${err} (retrieving source information...)`,
      {level: 'error'});

    bundleError.addSrcInfo(err).then(err1 => {
      pp(err1.srcSnip, `(${note}) ${err1}`, {level: 'error'});
    }).catch(err2 => {
      p(`${_info(err2)}`, `(${note}) ${err2}`, {level: 'error'});
    });
  } else {
    p(`${_info(err)}`, `(${note}) ${err}`, {level: 'error'});
  }
}

export default _reportError;

@@ -47,22 +47,48 @@
 ';%0A%0A
-function _info
+let _errorId = 1;%0A%0Afunction _formatError
 (err
@@ -358,34 +358,73 @@
 )%0A  ) %7B%0A    
-p(%60$%7B_info
+err._errorId = _errorId++;%0A%0A    p(%60$%7B_formatError
 (err)%7D%60,%0A   
@@ -427,32 +427,54 @@
 %0A      %60($%7Bnote%7D
+ (id: $%7Berr._errorId%7D)
 ) $%7Berr%7D (retrie
@@ -598,16 +598,24 @@
 srcSnip,
+%0A       
  %60($%7Bnot
@@ -612,24 +612,46 @@
    %60($%7Bnote%7D
+ (id: $%7Berr._errorId%7D)
 ) $%7Berr1%7D%60, 
@@ -649,16 +649,24 @@
 %7Berr1%7D%60,
+%0A       
  %7Blevel:
@@ -708,28 +708,35 @@
       p(%60$%7B_
-in
 fo
+rmatError
 (err2)%7D%60, %60(
@@ -728,24 +728,32 @@
 ror(err2)%7D%60,
+%0A       
  %60($%7Bnote%7D) 
@@ -746,24 +746,46 @@
    %60($%7Bnote%7D
+ (id: $%7Berr._errorId%7D)
 ) $%7Berr2%7D%60, 
@@ -783,16 +783,24 @@
 %7Berr2%7D%60,
+%0A       
  %7Blevel:
@@ -844,20 +844,27 @@
 %60$%7B_
-in
 fo
+rmatError
 (err)%7D%60,
  %60($
@@ -859,16 +859,22 @@
 (err)%7D%60,
+%0A     
  %60($%7Bnot
@@ -885,16 +885,22 @@
 $%7Berr%7D%60,
+%0A     
  %7Blevel:

Tweak error reporting code