B4J Question Error handling will not work

Siam

Active Member
Licensed User
Longtime User
Hi,

I have written a quick and (maybe) dirty program which connect to my 3D Printer to get some information.

My problem is that i sometimes get connection errors like time out or connection resets.

I have tried to catch those with try & Catch and with the Application_Error sub but without success can somebody point me in the right direction to catch these errors ?

B4X:
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private srvr As Server
    Dim lastLayer As Int = 0
    Dim lastPrinterState As String
    Dim LayerTimer As Timer
    Dim PrinterStateTimer As Timer
  
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
'    srvr.Port = 51042
'    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
'    srvr.AddHandler("/guessmynumber/guess", "GuessMyNumber", False)
'    srvr.AddHandler("/guessmynumber/reset", "ResetMyNumber", False)
'    srvr.AddWebSocket("/guessmynumber_ws/ws", "WSGuessMyNumber")
'    srvr.Start

    PrinterStateTimer.Initialize("status",500)
    PrinterStateTimer.Enabled=True

    LayerTimer.Initialize("Layer",250)
    StartMessageLoop
End Sub

Sub layer_tick
'    Try
    Wait For (getlayer) complete (result As Int)
    If result <> -1 Then
        If lastLayer <> result Then
            Log ("Layer: "&result)
            lastLayer=result
        End If
    End If
'    Catch
'        Log("Layertick")
'    End Try
End Sub

Sub status_tick
'    Try
        Dim j As HttpJob
        j.Initialize("", Me)
       
        j.Download("http://192.168.188.36/rr_model?key=state.status")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim parser As JSONParser
            parser.Initialize(j.GetString)
            Dim root As Map = parser.NextObject
            Dim PrinterState As String = root.Get("result")
            If lastPrinterState <> PrinterState Then
                Log(PrinterState)
                lastPrinterState=PrinterState
            End If
      
        Else
            Log ("status error "&j.ErrorMessage)
        End If
        j.Release
        If PrinterState <> "idle" And PrinterState <> "busy" And PrinterState <>"halted" Then 'processing
            LayerTimer.Enabled=True
        Else
            LayerTimer.Enabled=False
        End If
'    Catch
'        Log ("error can't get Printer status")
'    End Try
End Sub

Sub getlayer As ResumableSub
'    Try
    Dim j As HttpJob
    j.Initialize("", Me)
   
    j.Download("http://192.168.188.36/rr_model?key=job.layer")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim Layer As Int = root.Get("result")
        j.Release
        Return Layer
    Else
        Log ("getlayer error "&j.ErrorMessage)
    End If

'    Catch
'        Log ("error can't get Layer")
'        End Try
    Return -1
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Log ("app error")
'    If srvr.CurrentThreadIndex = 0 Then 'main thread
'        LogError(StackTrace)
'        Return False
''    Else 'handlers threads
''        Return True
'    End If
    Log (Error)
    Return False
End Sub

and here an error

B4X:
2021-02-08 21:48:59.048:INFO::main: Logging initialized @326ms to org.eclipse.jetty.util.log.StdErrLog
busy
idle
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at okio.Okio$2.read(Okio.java:140)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
    at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
    at okhttp3.RealCall.execute(RealCall.java:93)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
ResponseError. Reason: java.net.SocketException: Connection reset, Response:
status error java.net.SocketException: Connection reset

and a other error which i have to catch

B4X:
java.net.ConnectException: Failed to connect to /192.168.188.36:80
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:249)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
    at okhttp3.RealCall.execute(RealCall.java:93)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
    ... 25 more
ResponseError. Reason: java.net.ConnectException: Failed to connect to /192.168.188.36:80, Response:
getlayer error java.net.ConnectException: Failed to connect to /192.168.188.36:80
 
Last edited:

Siam

Active Member
Licensed User
Longtime User
Not really this is the output in release mode to the console

B4X:
G:\coding\b4j\d-lapse\Objects>java -jar result.jar
2021-02-09 12:26:37.179:INFO::main: Logging initialized @269ms to org.eclipse.jetty.util.log.StdErrLog
idle
java.net.SocketTimeoutException: timeout
        at okio.Okio$4.newTimeoutException(Okio.java:239)
        at okio.AsyncTimeout.exit(AsyncTimeout.java:286)
        at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
        at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
        at okhttp3.RealCall.execute(RealCall.java:93)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at okio.Okio$2.read(Okio.java:140)
        at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
        ... 27 more
ResponseError. Reason: java.net.SocketTimeoutException: timeout, Response:
status error java.net.SocketTimeoutException: timeout
processing
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at okio.Okio$2.read(Okio.java:140)
        at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
        at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
        at okhttp3.RealCall.execute(RealCall.java:93)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
ResponseError. Reason: java.net.SocketException: Connection reset, Response:
getlayer error java.net.SocketException: Connection reset
 
Upvote 0

Siam

Active Member
Licensed User
Longtime User
What is it that you want to "hide"? If the app is running OK and the user is happy the fact that there are messages in the logs doesn't matter.
If the messages were in a log file I would be happy but the error messages appear in the console and that's garbage.
By try and catch I mean that error messages can be caught before the end user. But for now it looks like for me that this is not possible with b4j
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Instead of logging to IDE, you can write the output to text file. What I see is you set the job calling every fraction of second while a job waitfor may took more than 2 seconds. Is it possible you set the timer duration to longer time Or totally get rid of using any timer?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Create an UI app and you don't see any log message (if you don't want to). :)
Or if you run the released jar from command, just let it run in the background.
 

Attachments

  • 3DPrinterSimulator.zip
    1.6 KB · Views: 299
  • 3DPrinterMonitor.zip
    1.6 KB · Views: 286
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks for your example! it seems that this code run a bit more stable as mine so i will "play" a bit with it to check if its fast enough for my app
For the client app, it doesn't need to be a jServer. So what I have done is using okhttputils wait for and sleep to pause the job. You can see I didn't use timer. I also purposely create a timeout to simulate the error at the server.
 
Upvote 0

Siam

Active Member
Licensed User
Longtime User
For now all works as it should! And i found a solution to hide the ugli ;) error messages from the console:

 
Upvote 0
Top