# Help with register\_error\_hint

**URL:** <https://discourse.julialang.org/t/help-with-register-error-hint/56140>\
**Category:** General Usage\
**Tags:** exception\
**Created:** [February 27, 2021, 2:28pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140 "2021-02-27T14:28:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [February 27, 2021, 2:28pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/1 "2021-02-27T14:28:49Z")

</div>

Hi, I want to add some useful hints but for some reason I can’t trigger the  
callback for a `UndefVarError` exception.

```julia
module Hinter

    only_int(x::Int) = 1
    any_number(x::Number) = 2

    function __init__ ()

        empty!(Base.Experimental._hint_handlers)

        # Documentation example
        Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
            if exc.f == only_int
                # Color is not necessary, this is just to show it's possible.
                print(io, "\nDid you mean to call ")
                printstyled(io, "`any_number`?", color=:cyan)
            end
        end

        # Extra hint
        Base.Experimental.register_error_hint(UndefVarError) do io, exc, argtypes, kwargs
            printstyled(io, "\nHi, `UndefVarError`!", color=:cyan)
        end

    end

end

## ----------------------------------------------------------------------------------------
# Documentation exmaple is working
Hinter.only_int(1.0)
#=
ERROR: LoadError: MethodError: no method matching only_int(::Float64)
HINT: Did you mean to call `any_number`?
Closest candidates are:
  only_int(::Int64) at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:34
Stacktrace:
 [1] top-level scope at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:64
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1088
in expression starting at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:64
=#

## ----------------------------------------------------------------------------------------
# My custom custom hint isn't. 
# I hit an UndefVarError, but is failing to hint
Hinter.only_int2(1.0)
#=
ERROR: LoadError: UndefVarError: only_int2 not defined
Stacktrace:
 [1] getproperty(::Module, ::Symbol) at ./Base.jl:26
 [2] top-level scope at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:69
 [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1088
in expression starting at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:69
=#

## ----------------------------------------------------------------------------------------
# The dictionary is populated 
Base.Experimental._hint_handlers
#=
IdDict{Type,Array{Any,1}} with 2 entries:
  MethodError => Any[#1]
  UndefVarError => Any[#2]
=#

## ----------------------------------------------------------------------------------------
# I can call my function
Base.Experimental._hint_handlers[UndefVarError][1](stdout, :bla, :bla, :bla)
#=
Hi, `UndefVarError`!
=#

```

Any thought what am i missing?  
Thank

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 27, 2021, 3:04pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/2 "2021-02-27T15:04:52Z")

</div>

This is just a guess, but it can be related to the fact your `UndefVarError` is being wrapped in a `LoadError`, so you had to add a hint to a `LoadError` that only happens if it wraps a `UndefVarError`.

I already had problems with this behavior (`LoadError` wrapping) when I was designing a timeout system (basically, in a series of runs for a scientific experiment, I wanted to throw `TimeoutException`s when runs passed the time limit, but this should be captured and not stop the process because some timeouts are expected). I think the wrapping happens if the exception is thrown inside a macro or something like that (there are some cases). I recommend searching for more info on `LoadError`.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [February 27, 2021, 3:14pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/3 "2021-02-27T15:14:06Z")

</div>

Hi, thank for the quick reply. I actually already tried that but I did not included in the MWE.

I actually put all in a `try` statement, check the type of the error and `rethrow` it and nothing.

```julia
# checking err type
try
    Hinter.only_int2(1.0)
catch err
    @show typeof(err)
    rethrow(err)
end
#=
typeof(err) = UndefVarError
ERROR: LoadError: UndefVarError: only_int2 not defined
# No hint
Stacktrace:
 [1] getproperty(::Module, ::Symbol) at ./Base.jl:26
 [2] top-level scope at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:113
 [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1088
in expression starting at /Users/Pereiro/.julia/dev/DrWatson/dev/dev.jl:112
=#

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 27, 2021, 3:18pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/4 "2021-02-27T15:18:38Z")

</div>

This is because `showerror` for `UndefVarError` does not currently call back into `show_error_hints`. It is still an experimental feature, so it isn’t implemented consistently by all exceptions yet. I actually did implement this as part of [https://github.com/JuliaLang/julia/pull/39089](https://github.com/JuliaLang/julia/pull/39089), so if you want to make a PR, it should be quite straightforward to extract that part.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [February 27, 2021, 4:14pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/5 "2021-02-27T16:14:40Z")

</div>

Hi thanks, I made the pull request [#39850](https://github.com/JuliaLang/julia/pull/39850)

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [April 16, 2021, 6:34pm UTC](https://discourse.julialang.org/t/help-with-register-error-hint/56140/6 "2021-04-16T18:34:45Z")

</div>

This was merged in v1.6! Urra!
