# Nice macro errors with JuliaSyntax

**URL:** <https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017>\
**Category:** Internals & Design\
**Tags:** macros, error-message\
**Created:** [March 23, 2024, 1:08pm UTC](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017 "2024-03-23T13:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [March 23, 2024, 1:08pm UTC](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017/1 "2024-03-23T13:08:13Z")

</div>

Now with the introduce of `JuliaSyntax` in newer versions of julia, we get much more friendly syntax error messages. For example:

```julia
julia> foo ()
ERROR: ParseError:
# Error @ REPL[28]:1:4
foo ()
# ╙ ── whitespace is not allowed here
Stacktrace:
 [1] top-level scope
   @ none:1

```

Now, i’m wondering if we could have these nice error messages in macros. For instance, a macro may require a particular syntax/expression, and if provided with the wrong one, it will throw an error:

```julia
macro singleton(expr::Expr)
    isdef(expr) || throw("Expected a function declaration")
    ...
end

```

I usually call `throw(something)` explaining to the user what went wrong, which result in an error like:

```julia
julia> @singleton x^2
ERROR: LoadError: "Expected a function declaration"
Stacktrace:
 [1] var"@singleton"( __source__ ::LineNumberNode, __module__ ::Module, expr::Expr)
   @ Main .\REPL[38]:2
in expression starting at REPL[41]:1

```

But, what if we could have nicer errors:

```julia
julia> @singleton x^2
ERROR: SyntaxError:
# Error @ REPL[41]:1
@singleton x^2
# ╙ ── Expected function declaration
Stacktrace:
 [1] var"@singleton"( __source__ ::LineNumberNode, __module__ ::Module, expr::Expr)
   @ Main .\REPL[38]:2
in expression starting at REPL[41]:1

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 23, 2024, 2:36pm UTC](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017/2 "2024-03-23T14:36:02Z")

</div>

I think that is what `Diagnostics` does in JuliaSyntax:

> <https://github.com/JuliaLang/JuliaSyntax.jl/blob/main/src%2Fdiagnostics.jl>

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [March 23, 2024, 9:32pm UTC](https://discourse.julialang.org/t/nice-macro-errors-with-juliasyntax/112017/3 "2024-03-23T21:32:08Z")

</div>

`Diagnostics` seems to be what i’m looking for, but the problem is finding the position in the code where the macro is called along with its arguments. Right now, i only managed to use Diagnostics to highlight the entire line.
