-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As mentioned in #10, you cannot use a TwiML verb name as a parameter in a function definition, like this: ```elixir Enum.each [1, 2] fn(number) -> say "Press #{number}" end ``` This commit adds a nice warning message if a user attempts to do this, telling them that the "number" verb is reserved.
- Loading branch information
1 parent
07f42b3
commit c138636
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule ExTwiml.ReservedNameError do | ||
@moduledoc """ | ||
This error is thrown if you try to use TwiML verb name as a variable name | ||
in your `twiml` block. | ||
## Example | ||
This code will raise the error, because `number` is a reserved name. | ||
twiml do | ||
Enum.each [1, 2], fn(number) -> | ||
# ... | ||
end | ||
end | ||
""" | ||
|
||
defexception [:message] | ||
|
||
@doc false | ||
def exception([{name, context, _}, file_name]) do | ||
file_name = Path.relative_to_cwd(file_name) | ||
name = to_string(name) | ||
|
||
message = ~s""" | ||
"#{name}" is a reserved name in #{file_name}:#{context[:line]}, because it | ||
is used to generate the <#{String.capitalize(name)} /> TwiML verb. | ||
Please use a different variable name. | ||
""" | ||
|
||
%__MODULE__{message: message} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule ExTwiml.ReservedNameErrorTest do | ||
use ExUnit.Case | ||
|
||
alias ExTwiml.ReservedNameError | ||
|
||
test ".exception returns a nice exception" do | ||
%{message: message} = ReservedNameError.exception([{:number, [line: 1], []}, | ||
"test/test.ex"]) | ||
|
||
assert message == ~s""" | ||
"number" is a reserved name in test/test.ex:1, because it | ||
is used to generate the <Number /> TwiML verb. | ||
Please use a different variable name. | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters