Skip to content

Commit

Permalink
Catch the invalid elixir syntax error in smart contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed May 3, 2023
1 parent 2b2351a commit d5f4e77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ defmodule Archethic.Contracts.Interpreter do
|> check_contract_blocks()
end

{:error, :invalid_syntax} ->
{:error, "Parse error: invalid language syntax"}

{:error, {[line: line, column: column], _msg_info, _token}} ->
{:error, "Parse error at line #{line} column #{column}"}
end
Expand Down Expand Up @@ -77,9 +80,16 @@ defmodule Archethic.Contracts.Interpreter do
"""
@spec sanitize_code(binary()) :: {:ok, Macro.t()} | {:error, any()}
def sanitize_code(code) when is_binary(code) do
code
|> String.trim()
|> Code.string_to_quoted(static_atoms_encoder: &atom_encoder/2)
try do
code
|> String.trim()
|> Code.string_to_quoted(static_atoms_encoder: &atom_encoder/2)
rescue
_ ->
# catch the non-elixir syntax here
# example [key:value] is not valid
{:error, :invalid_syntax}
end
end

@doc """
Expand Down
11 changes: 11 additions & 0 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ defmodule Archethic.Contracts.InterpreterTest do
"""
|> Interpreter.parse()
end

test "should return an human readable error if syntax is not elixir-valid" do
assert {:error, "Parse error: invalid language syntax"} =
"""
@version 1
actions triggered_by:transaction do
x = "missing space above"
end
"""
|> Interpreter.parse()
end
end

describe "parse code v0" do
Expand Down

0 comments on commit d5f4e77

Please sign in to comment.