I propose that we add `IO.error/2` which matches the signature of `IO.warn/2`. It emits error diagnostics/messages instead of warnings and it causes the compile step to finish with :error.
Reasoning
Often when building a macro you'll do some validation and `raise` an error if something isn't right. This creates a poor experience for the person using the macro for two reasons:
1. You can only raise one error at a time
2. You don't get a good picture of where the error is because your whole editor turns red
You can solve both of those problems by using `IO.warn/2`. You can emit multiple warnings in a single compile step and you can pass in a stacktrace which gets turned into a Compiler Diagnostic that in turn, creates good editor hints. But now the compilation succeeds and you're left in a bad state.
I think it is useful to see multiple errors at a time because it shortens the feedback loop. It also gives more context and can help you realize where the root cause of your issue lies.
I think it would be useful to have a function that shared the properties of `IO.warn/2` and `raise/1`:
- I can emit multiple messages during a single compilation run
- These messages are output as Compiler Diagnostic errors
- Invoking this function will ultimately cause the compilation step to result in an :error
Examples
Today in Ecto Query, this snippet will cause the entire file to turn red because we mistakenly used sort_by instead of order_by.
query p in Product, sort_by: p.price
Lets assume we forgot to load the :user type in this Absinthe Schema. We have two errors but only one gets displayed. Once again, the entire editor turns red. If instead we saw each line that referenced :user turn red it might remind us more quickly that we forgot to call `import_types MyApp.UserTypes`.
query name: "Query" do
field :user, :user, resolve: &MyApp.Users.get_user/3
...
field :users, list_of(:user), resolve: &MyApp.Users.list_user/3
...
end
I'm currently working on a library to create GraphQL queries. I can detect three errors in the following snippet. Today I can only report one at a time and am greeted with the wall of red. If I switch to `IO.warn/2` I can report them all and see nice editor hints but my user is left with broken code after compile.
query do
user(id: "should_be_a_number") do # wrong type for id
firstNam # misspelled, should be firstName
lastName do # lastName should not have a do...end block
name
end
end
end
Code
Here is a patch that adds `IO.error/2`: