Skip to content

Commit d0bb38d

Browse files
committed
fix: variable name in undefined variable exceptin message
1 parent cf17a0b commit d0bb38d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/solid/undefined_variable_error.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ defmodule Solid.UndefinedVariableError do
44

55
@impl true
66
def message(exception) do
7+
variable = exception.variable
8+
9+
variable_name =
10+
if is_list(variable) do
11+
Enum.join(variable, ".")
12+
else
13+
variable
14+
end
15+
716
line = exception.loc.line
8-
reason = "Undefined variable #{exception.variable}"
17+
reason = "Undefined variable #{variable_name}"
918
"#{line}: #{reason}"
1019
end
1120
end

test/solid_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,26 @@ defmodule SolidTest do
268268
}
269269
]
270270
end
271+
272+
test "undefined variable error message with multiple variables" do
273+
template = "{{ var1 }}\n{{ event.name }}\n{{ user.properties.name }}"
274+
275+
{:error, [first_error, second_error, third_error], _partial_result} =
276+
template
277+
|> Solid.parse!()
278+
|> Solid.render(%{}, strict_variables: true, file_system: {TestFileSystem, nil})
279+
280+
assert String.contains?(Solid.UndefinedVariableError.message(first_error), "var1")
281+
282+
assert String.contains?(
283+
Solid.UndefinedVariableError.message(second_error),
284+
"event.name"
285+
)
286+
287+
assert String.contains?(
288+
Solid.UndefinedVariableError.message(third_error),
289+
"user.properties.name"
290+
)
291+
end
271292
end
272293
end

0 commit comments

Comments
 (0)