Skip to content

Commit

Permalink
Merge pull request #371 from permaweb/jfrain99/fix-error-line-number
Browse files Browse the repository at this point in the history
fix(aos): fix error line number in aos
  • Loading branch information
twilson63 authored Oct 17, 2024
2 parents bcf99b5 + ee4a4f5 commit 4863f15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ async function doEvaluate(line, id, jwk, spinner, rl, loadedModules, dryRunMode)
// get what file the error comes from,
// if the line was loaded
const errorOrigin = getErrorOrigin(loadedModules, error.lineNumber)

// print error
outputError(line, error, errorOrigin)
} else {
Expand Down
41 changes: 25 additions & 16 deletions src/services/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ export function getErrorOrigin(loadedModules, lineNumber) {
let currentLine = 0

for (let i = 0; i < loadedModules.length; i++) {
// get module line count
const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1

// get module line count, add 2 for '\n\n' offset
const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1 + 2
if (currentLine + lineCount >= lineNumber) {
return {
file: loadedModules[i].path,
line: lineNumber - currentLine - i * 2
line: lineNumber - currentLine - (i + 1) * 2
}
}

Expand All @@ -92,18 +91,28 @@ export function getErrorOrigin(loadedModules, lineNumber) {
* @param {ErrorOrigin|undefined} origin
*/
export function outputError(line, error, origin) {
const lineNumber = origin?.line || error.lineNumber
// Subtract 2 lines as the line does not includes the '\n\n' offset
const lineNumber = (origin?.line || error.lineNumber - 2)
const lineNumberPlaceholder = ' '.repeat(lineNumber.toString().length)

console.log(
'\n' +
chalk.bold(error.errorMessage) +
'\n' +
(origin ? chalk.dim(` in ${origin.file}\n`) : "") +
chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) +
line.split('\n')[error.lineNumber - 1] +
'\n' +
chalk.blue(` ${lineNumberPlaceholder} |\n`) +
chalk.dim('This error occurred while aos was evaluating the submitted code.')
)
if (origin) {
console.log(
'\n' +
chalk.bold(error.errorMessage) +
'\n' +
(origin ? chalk.dim(` in ${origin.file}\n`) : "") +
chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) +
line.split('\n')[lineNumber + 1] +
'\n' +
chalk.blue(` ${lineNumberPlaceholder} |\n`) +
chalk.dim('This error occurred while aos was evaluating the submitted code.')
)
} else {
console.log(
'\n' +
chalk.bold(`Error on line ${lineNumber}: ${error.errorMessage}`) +
'\n' +
chalk.dim('This error occurred while aos was evaluating the submitted code.')
)
}
}

0 comments on commit 4863f15

Please sign in to comment.