| 25 | * @param {string} [currentFilename] |
| 26 | */ |
| 27 | const LessError = function(e, fileContentMap, currentFilename) { |
| 28 | Error.call(this); |
| 29 | |
| 30 | const filename = e.filename || currentFilename; |
| 31 | |
| 32 | this.message = e.message; |
| 33 | this.stack = e.stack; |
| 34 | |
| 35 | // Set type early so it's always available, even if fileContentMap is missing |
| 36 | this.type = e.type || 'Syntax'; |
| 37 | |
| 38 | if (fileContentMap && filename) { |
| 39 | const input = fileContentMap.contents[filename]; |
| 40 | const loc = utils.getLocation(e.index, input); |
| 41 | var line = loc.line; |
| 42 | const col = loc.column; |
| 43 | const callLine = e.call && utils.getLocation(e.call, input).line; |
| 44 | const lines = input ? input.split('\n') : ''; |
| 45 | |
| 46 | this.filename = filename; |
| 47 | this.index = e.index; |
| 48 | this.line = typeof line === 'number' ? line + 1 : null; |
| 49 | this.column = col; |
| 50 | |
| 51 | if (!this.line && this.stack) { |
| 52 | const found = this.stack.match(anonymousFunc); |
| 53 | |
| 54 | /** |
| 55 | * We have to figure out how this environment stringifies anonymous functions |
| 56 | * so we can correctly map plugin errors. |
| 57 | * |
| 58 | * Note, in Node 8, the output of anonymous funcs varied based on parameters |
| 59 | * being present or not, so we inject dummy params. |
| 60 | */ |
| 61 | const func = new Function('a', 'throw new Error()'); |
| 62 | let lineAdjust = 0; |
| 63 | try { |
| 64 | func(); |
| 65 | } catch (e) { |
| 66 | const match = e.stack.match(anonymousFunc); |
| 67 | lineAdjust = 1 - parseInt(match[2]); |
| 68 | } |
| 69 | |
| 70 | if (found) { |
| 71 | if (found[2]) { |
| 72 | this.line = parseInt(found[2]) + lineAdjust; |
| 73 | } |
| 74 | if (found[3]) { |
| 75 | this.column = parseInt(found[3]); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | this.callLine = callLine + 1; |
| 81 | this.callExtract = lines[callLine]; |
| 82 | |
| 83 | this.extract = [ |
| 84 | lines[this.line - 2], |