Error (the Class) vs ERROR (the Boolean)

Post your Gambas programming questions here.
Post Reply
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Error (the Class) vs ERROR (the Boolean)

Post by sjsepan »

I wrote a function FormatError to format the info in the Error class, taking oError as Error, but when passing it to the function, Gambas thinks I am passing the ERROR boolean value. What am I doing wrong? Is it because Error (class) is static?
_______________

Code: Select all

' Gambas module file


Public Sub Main()

  Print "Hello world"
  Print 1 / 0
  Catch
      ' Debug Error.Text    'OK
      Debug FormatError( Error )  'NOT OK:  not using ByRef; 'wanted Error got Boolean'
      'Debug FormatError(ByRef Error )  'NOT OK: using ByRef; 'this expression cannot be passed by reference'

End

Public Function FormatError(oError As Error) As String
    Dim sResult As String = Null
    sResult &= oError.Text & gb.CrLf & Error.Class & gb.CrLf & Error.Where & gb.CrLf & Error.Backtrace 
    Return sResult
    
End
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Error (the Class) vs ERROR (the Boolean)

Post by sjsepan »

OK, tried a work-around: passed properties individually, got 'type mismatch: wanted string got class instead'

Code: Select all

' Gambas module file


Public Sub Main()

  Print "Hello world"
  Print 1 / 0
  Catch
      Debug FormatError(Error.Text, Error.Class, Error.Where, Error.Backtrace)  'NOT OK: FormatError not using ByRef; wanted Error got Boolean

End

Public Function FormatError(sText As String, sClass As String, sWhere As String, sBacktrace As String) As String 
    Dim sResult As String = Null
    sResult &= sText & gb.CrLf & sClass & gb.CrLf & sWhere & gb.CrLf & sBacktrace 
    Return sResult
    
End
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Error (the Class) vs ERROR (the Boolean)

Post by sjsepan »

environment:
[System]
Gambas=3.14
OperatingSystem=Linux
Kernel=4.15.0-65-generic
Architecture=x86_64
Distribution=Linux Mint 19.2 Tina
Desktop=CINNAMON
Theme=Qt5CTProxy
Language=en_US.UTF-8
Memory=7917M

[Libraries]
Cairo=libcairo.so.2.11510.0
Curl=libcurl.so.4.5.0
DBus=libdbus-1.so.3.19.4
GStreamer=libgstreamer-1.0.so.0.1405.0
GTK+2=libgtk-x11-2.0.so.0.2400.32
GTK+3=libgtk-3.so.0.2200.30
OpenGL=libGL.so.1.0.0
Poppler=libpoppler.so.73.0.0
QT4=libQtCore.so.4.8.7
QT5=libQt5Core.so.5.9.5
SDL=libSDL-1.2.so.0.11.4
SQLite=libsqlite3.so.0.8.6

[Environment]
CINNAMON_VERSION=4.2.4
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DEFAULTS_PATH=/usr/share/gconf/cinnamon.default.path
DESKTOP_SESSION=cinnamon
DISPLAY=:0
GB_GUI=gb.qt5
GDMSESSION=cinnamon
GDM_LANG=en_US
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/gambas3.desktop
GIO_LAUNCHED_DESKTOP_FILE_PID=30177
GJS_DEBUG_OUTPUT=stderr
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GTK_MODULES=gail:atk-bridge
GTK_OVERLAY_SCROLLING=1
HOME=<home>
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LOGNAME=<user>
MANDATORY_PATH=/usr/share/gconf/cinnamon.mandatory.path
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:<home>/.dotnet/tools
PWD=<home>
QT_ACCESSIBILITY=1
QT_QPA_PLATFORMTHEME=qt5ct
SESSION_MANAGER=local/<hostname>:@/tmp/.ICE-unix/2063,unix/<hostname>:/tmp/.ICE-unix/2063
SHELL=/bin/bash
SHLVL=0
SSH_AGENT_PID=2154
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
S_COLORS=auto
TZ=:/etc/localtime
USER=<user>
XAUTHORITY=<home>/.Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-cinnamon:/etc/xdg
XDG_CURRENT_DESKTOP=X-Cinnamon
XDG_DATA_DIRS=/usr/share/cinnamon:/usr/share/gnome:<home>/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/<user>
XDG_RUNTIME_DIR=/run/user/1000
XDG_SEAT=seat0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_SESSION_DESKTOP=cinnamon
XDG_SESSION_ID=c2
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SESSION_TYPE=x11
XDG_VTNR=7
User avatar
gbWilly
Posts: 68
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: Error (the Class) vs ERROR (the Boolean)

Post by gbWilly »

Hi sjsepan,

There is no need to pass Error (the class) as you can approach it directly.

Try this:

Public Sub Main()
  
    Print 1 / 0
    Catch
    
       Debug FormatError() 
  
End


Public Function FormatError() As String

    Dim sResult, sLine As String
    
    sResult = Subst("Error at &1: (&2) &3\n", Error.Where, Error.Code, Error.Text)
    sResult &= "Backtrace:\n"
    For Each sLine In Error.Backtrace
       sResult &= sLine & "\n"
    Next
    
    Return sResult
    
End

Take into account that:
- Error.Class returns a class NOT a string and will give you an error (Type mismatch: wanted String, got Class instead) if trying to get it returned as a string.
- Error.Backtrace will return an array of strings and will give an error if trying to get it returned in your string result.

Hope this short example will help you.
gbWilly
- Dutch translation for Gambas3
- Gambas wiki content contributer


... there is always a Catch if things go wrong!
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Error (the Class) vs ERROR (the Boolean)

Post by sjsepan »

gbWilly, Thanks! I was considering trying that approach (directly using the static class) next, but forgot about it while getting myself lost in the other detail.
I definitely overlooked the types on Class and Backtrace -- I have to look more carefully. :oops:
Steve
cage
Posts: 123
Joined: Monday 2nd September 2019 5:47am
Location: Phoenix Arizona

Re: Error (the Class) vs ERROR (the Boolean)

Post by cage »

Greeting gbWilly been a long time since I have talked to you. Your error example is a really great one and I will be using it in future programs. It's a great way to debug a program and know exactly where the problem is at. Thank you as always for another good example.
User avatar
gbWilly
Posts: 68
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: Error (the Class) vs ERROR (the Boolean)

Post by gbWilly »

Glad I can be off help.

@cage: where do we know each other from, the old forum perhaps and under a different name?
gbWilly
- Dutch translation for Gambas3
- Gambas wiki content contributer


... there is always a Catch if things go wrong!
cage
Posts: 123
Joined: Monday 2nd September 2019 5:47am
Location: Phoenix Arizona

Re: Error (the Class) vs ERROR (the Boolean)

Post by cage »

Yeah it's been years since we last talked. It was on a different forum that I don't think exists anymore. I was working on a secure delete program but needed to have a sudo ability. You came up with a routine to use sudo that really worked. As for the name I use I really don't remember since it's been so long. It 's good to see your still around :D
Post Reply