Error message not printed

Hi guys, I am actually continuing to work of stabilizing my package manager written in crystal.

I am facing actually an issue with my catching error system I coded.

I did a smaller example of the issue I have.

Basically, when you run this code, it do not show the message “Privilege escalation failure“, why ?

lib LibC
    fun setresgid(realId : UidT, effectiveId : UidT, savedId : UidT): Int
    fun setresuid(realId : UidT, effectiveId : UidT, savedId : UidT): Int
end

def showError(  className : String,
                functionName : String,
                errorTitle : String,
                error : String,
                exception = Exception.new,
                information = String.new,
                errorCode = 1)
            fullLog = (exception.backtrace.empty? ? exception.backtrace.join("\n") : exception.message)

            title = "Error"

            errorText = "#{fullLog}"
            help = "Fix the code"

            errorReport = <<-REPORT
            [ #{title} ]

            Class: #{className}
            Function: #{functionName}

            #{errorTitle}
            #{error}

            Exception:
            #{errorText}

            Exit code: #{errorCode}

            #{help}
            REPORT

            puts "\n#{errorReport}\n"

    exit errorCode
end

begin
uidResult = LibC.setresuid( realId: 0,
                            effectiveId: 0,
                            savedId: 250)
gidResult = LibC.setresgid( realId: 0,
                            effectiveId: 0,
                            savedId: 250)

if uidResult.negative? || gidResult.negative?
    showError(  className: "CommandLine",
                functionName: "runAsSuperUser",
                errorTitle: "Privilege escalation failure",
                error: "It mean probably that the uid and gid bit are not set.")
end

rescue exception
    puts exception.class
end

On line 13, exception.backtrace is raising a NilAssertionError because there is no backtrace. Probably want to use #backtrace? to handle this case.

1 Like

Omg thank you so much. I code so much that sometimes I finish to see nothing. :neutral_face: Thanks a lot @Blacksmoke16