Error (the Class) vs ERROR (the Boolean)

Post

Posted
Rating:
#1 (In Topic #327)
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
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

' 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
OK, tried a work-around: passed properties individually, got 'type mismatch: wanted string got class instead'

Code

' 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
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
Hi sjsepan,

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

Try this:

Code (gambas)

  1.  
  2. Public Sub Main()
  3.  
  4.     Print 1 / 0
  5.     Catch
  6.    
  7.        Debug FormatError()
  8.  
  9.  
  10.  
  11. Public Function FormatError() As String
  12.  
  13.     Dim sResult, sLine As String
  14.    
  15.     sResult = Subst("Error at &1: (&2) &3\n", Error.Where, Error.Code, Error.Text)
  16.     sResult &= "Backtrace:\n"
  17.     For Each sLine In Error.Backtrace
  18.        sResult &= sLine & "\n"
  19.     Next
  20.    
  21.     Return sResult
  22.    
  23.  
  24.  

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
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
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
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
cage is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
 Glad I can be off help.

cage : where do we know each other from, the old forum perhaps and under a different name?

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
cage is in the usergroup ‘Regular’
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
Online now: No Back to the top
1 guest and 0 members have just viewed this.