Page 1 of 1

Specialized error handling

Posted: Tue Oct 11, 2016 8:07 pm
by Cliff Wiernik
I have several instances where a user defined query is entered into a character string variable. The query is then used to control selection of records during a process navigating through the database. Because the user can make a mistake, I employ a two step process.

Test query format of the query, cInputExp:

bError := ErrorBlock( {|e| Break(e)} ) // install new error handling code block

BEGIN SEQUENCE
IF ( EMPTY( cInputExp ) )
xType := "U"
ELSE
xResult := &cInputExp.
xType := VALTYPE( xResult )
ENDIF
RECOVER USING e
xType := "U"
ENDSEQUENCE

ErrorBlock(bError) // reinstall old error handler

RETURN xType

This process checks the syntax of the query but cannot validate all variable names used because the the short-cut evaluation process of the macro compiler.

Then within the data extraction loop the following is done:

bSaveErrorBlock := ErrorBlock( {|e| Break(e) } )

BEGIN SEQUENCE

DO WHILE .......
IF eval(bQuery).......
ENDIF

SKIP
ENDDO

RECOVER

ErrorBlock(bSaveErrorBlock)

END SEQUENCE

The above code does capture the error and prevent a run time error but hides what the actual error is. I am wondering how to properly use the code to provide information as to what the error actually was and the program stack/line that caused the error. I see the example in the documentation using "RECOVER use oError". Are the any other examples of how to best provide this. I know Express++ does this when trapping errors in the DC_GetRefresh() process. Are there any other good examples of how best to use the Error object so the nature of the error can be provided back to the user to assist in correcting the query.

Re: Specialized error handling

Posted: Tue Oct 11, 2016 11:11 pm
by Auge_Ohr
Cliff Wiernik wrote:I see the example in the documentation using "RECOVER use oError".
Are the any other examples of how to best provide this.
do you have a XppError.LOG File ...
what oError Property exist is in your XppError.LOG like

oError:description
oError:operation
oError:args

Code: Select all

  BEGIN SEQUENCE
     FieldPut(nPos,xValue)
  RECOVER USING oError
     lRet := .F.
     IF NIL <> oError
        AADD(aError, {aStruct[i][DBS_NAME],cType,aStruct[i][DBS_LEN],aStruct[i][DBS_DEC],;
                  CHR(13)+CHR(10) ,;
                  xValue, oError:description } )
     ENDIF
  ENDSEQUENCE

Re: Specialized error handling

Posted: Wed Oct 12, 2016 6:22 am
by rdonnay
Try this:

Code: Select all

LOCAL bError := ErrorBlock( {|e| (DCMSGBOX 'Invalid expression:', cInputExp), Break(e)} ) 

Re: Specialized error handling

Posted: Wed Oct 12, 2016 1:25 pm
by Cliff Wiernik
I looked at the many places in express that do this. I will create something like used for dc_getrefresh(). I need to know specifics on the error, not just that it has an error. I do that already. The problems is this is just generic at this point. Need to know what items is not correct and on what line, in case some other error occurs. I don't want to loose all error information which is what I have now.

Thanks for the info.

Re: Specialized error handling

Posted: Wed Oct 12, 2016 1:38 pm
by rdonnay

Code: Select all

LOCAL bError := ErrorBlock( {|e| DC_InspectObject(e), Break(e)} )

Re: Specialized error handling

Posted: Wed Oct 12, 2016 8:44 pm
by Cliff Wiernik
Thanks. I will generate an error and look at what is available. Did not think about that.