Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
iHiD committed Jan 22, 2025
1 parent 97a01e6 commit 5b97d90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/javascript/interpreter/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Executor

if (statement.expression instanceof VariableExpression)
this.error('MissingParenthesesForFunctionCall', statement.location, {
expression: statement.expression,
name: statement.expression.name.lexeme,
})

return result
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/interpreter/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
},
"runtime": {
"CouldNotEvaluateFunction": "Could not evaluate function",
"CouldNotFindFunctionWithName": "Could not find function with name.",
"CouldNotFindFunctionWithNameSuggestion": "We don't know what `{{name}}` means. Maybe you meant to use the `{{suggestion}}` function instead?",
"CouldNotFindValueWithName": "Could not find value with name '{{name}}'.",
"CouldNotFindFunctionWithName": "Jiki couldn't find a function with the name `{{name}}`.",
"CouldNotFindFunctionWithNameSuggestion": "Jiki couldn't find a function with the name `{{name}}`. Maybe you meant to use the `{{suggestion}}` function instead?",
"CouldNotFindValueWithName": "Jiki couldn't find a variable or function with name `{{name}}`.",
"InfiniteLoop": "Your code ran for too long and we stopped it to prevent an infinite loop.",
"InvalidBinaryExpression": "Invalid binary expression",
"InvalidExpression": "Invalid expression",
Expand Down
33 changes: 22 additions & 11 deletions test/javascript/interpreter/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,6 @@ describe('frames', () => {
expect(frames[0].error).toBeNil()
expect(frames[0].variables).toBeEmpty()
})

test('not using brackets when calling a function', () => {
const { error, frames } = interpret(`
set x to y
`)
console.log(error, frames)
expect(frames[0].status).toBe('ERROR')
})
})

describe('multiple statements', () => {
Expand Down Expand Up @@ -1197,7 +1189,6 @@ describe('errors', () => {
],
}
const { frames } = interpret('set x to echo', context)
console.log(frames)
expect(frames[0].status).toBe('ERROR')
})

Expand Down Expand Up @@ -1281,7 +1272,7 @@ describe('errors', () => {
expect(frames[0].code).toBe('foobor()')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
"We don't know what `foobor` means. Maybe you meant to use the `foobar` function instead?"
"Jiki couldn't find a function with the name `foobor`. Maybe you meant to use the `foobar` function instead?"
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe(
Expand All @@ -1305,7 +1296,27 @@ describe('errors', () => {
expect(frames[0].code).toBe('foo')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to call the function?'
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```foo()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
expect(error).toBeNull()
})

test('missing parentheses within set', () => {
const context = {
externalFunctions: [
{ name: 'y', func: (_: any) => {}, description: '' },
],
}
const { error, frames } = interpret(`set x to y`, context)
expect(frames).toBeArrayOfSize(1)
expect(frames[0].line).toBe(1)
expect(frames[0].status).toBe('ERROR')
expect(frames[0].code).toBe('set x to y')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```y()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
Expand Down

0 comments on commit 5b97d90

Please sign in to comment.