Stack trace

I am using 2 functions related to stack to help me with debugging.

  1. getExcInfo
function getExcInfo(someText=nothing)::String
    msg::String = ""
    for (exc, bt) in Base.catch_stack()
        # for now, we process only 1st item (we don't expect nested try/catch blocks)
        msg = sprint(showerror, exc, bt) * "\n"
        sPrefix= isnothing(someText) ? "" : (someText *"\n")
        return sPrefix * msg
    end
    return ""
end

and I use it in the code in the catch block:

try
    some_code
catch
    @error getExcInfo()
end

Result look beautiful, this is an example:

MethodError: Cannot `convert` an object of type Int64 to an object of type String
Closest candidates are:
  convert(::Type{String}, !Matched::T) where T<:Union{AMQPClient.TAMQPByteArray, AMQPClient.TAMQPLongStr, AMQPClient.TAMQPShortStr} at X:\my_path\.julia\packages\AMQPClient\kT8x4\src\convert.jl:1
  convert(::Type{String}, !Matched::LibPQ.PQValue) at X:\my_path\.julia\packages\LibPQ\Ac0YY\src\parsing.jl:110
  convert(::Type{String}, !Matched::FilePathsBase.AbstractPath) at X:\my_path\.julia\packages\FilePathsBase\kMipS\src\path.jl:112        
  ...
Stacktrace:
 [1] uploadDataset(::LibPQ.Connection, ::Dict{String,Any}, ::Array{UInt8,1}) at X:\my_path\.julia\dev\some_app\src\Routes\WorkerUpload.jl:14
 [2] (::some_app.QueueConsumer.var"#1#2"{Dict{String,Any},Array{UInt8,1}})() at X:\my_path\.julia\dev\some_app\src\QueueConsumer.jl:73
 [3] with_logstate(::Function, ::Any) at .\logging.jl:408
 [4] with_logger at .\logging.jl:514 [inlined]
 [5] receiveData(::Dict{String,Any}, ::Array{UInt8,1}) at X:\my_path\.julia\dev\some_app\src\QueueConsumer.jl:58
 [6] _callBackWithResponse(::Dict{String,Any}, ::Array{UInt8,1}, ::Int64) at X:\my_path\.julia\packages\TIMCommon\tGcU0\src\AMQPQueue.jl:177
etc.

In VS Code I can click on the code reference to get me there. I love it!

  1. backtraceString
function backtraceString()::String
  st = stacktrace(backtrace())
  return join(string.(st), "\n")
end

I want to put this function call in the line where I want to see the full stack info, i.e.:

some_code_part_1
@info backtraceString()
some_code_part_2

It works a bit, but it is not good enough. Result looks like this:

backtraceString() at TIMUtils.jl:503
macro expansion at logging.jl:331 [inlined]
uploadDataset(::LibPQ.Connection, ::Dict{String,Any}, ::Array{UInt8,1}) at WorkerUpload.jl:13
(::msDatasetManagement.QueueConsumer.var"#1#2"{Dict{String,Any},Array{UInt8,1}})() at QueueConsumer.jl:73
with_logstate(::Function, ::Any) at logging.jl:408
with_logger at logging.jl:514 [inlined]
receiveData(::Dict{String,Any}, ::Array{UInt8,1}) at QueueConsumer.jl:58
_callBackWithResponse(::Dict{String,Any}, ::Array{UInt8,1}, ::Int64) at AMQPQueue.jl:177

There is no path here at all, only file names. Thus I cannot click on it as with getExcInfo and get to the code.
Would it be possible to get the full path(s) here?

Thank you

I came up with solution:

function backtraceString()::String
  st = stacktrace(backtrace())
  if !isempty(st) # remove this function from stack
    popfirst!(st)
  end
  if !isempty(st) && startswith(string(st[1]), "macro expansion at logging.jl")
    # if this function is called inside logging macro (which it most likely is) e.g. @info
    # remove that macro from stack as well
    popfirst!(st)
  end
  function oneFrame(frame::Base.StackTraces.StackFrame)
    return "$(frame.func) at $(frame.file):$(frame.line)$(frame.inlined ? " [inlined]" : "")"
  end
  return join(oneFrame.(st), "\n")
end