Wiki Home

Sample Error Handlers


Namespace: Wiki
Here is the Error Handler that I use when creating Small Demo Packages.
#Define SqlConStr "DRIVER=SQL Server;Network=DBMSSOCN;SERVER=xyz;UID=abc;PWD=def"
SQLSetProp(0,"DispWarnings", .F. )
SQLSetProp(0,"DispLogin",3)
lhX = Sqlstringconnect( SqlConStr )
lckErr( lhX, "connect" )
? SQLDisconnect(0)
Wait
Return

*************************************************************************
Function lckErr( tcErr, tcDsc )
	If !Empty( tcDsc )
		? tcDsc, Trans( tcErr )
	Endif
	If tcErr < 0
		xErrHlr()
	Endif
	Return .T.
Endfunc
*************************************************************************
Function xErrHlr( tnLin, tcLin )

	#Define CRLF Chr(13)+Chr(10)
	#Define CR_LF Chr(13) + Chr(10)

	Local ;
		lnErr, lnErr, ;
		llOleErr, llOdbcErr, llTrigErr, ;
		lcErrMsg

	Local ;
		array laErrs[1]

	If !Empty( tcLin )
		? "Line " + Transform( tnLin ) + ": " + Transform( tcLin )
	Endif

	For lnErr = 1 To Aerror( laErrs )

		llOleErr = Between( laErrs[lnErr, 1], 1426, 1429 )
		llOdbcErr = laErrs[lnErr, 1] = 1526
		llTrigErr = laErrs[lnErr, 1] = 1539

		lcErrMsg = ;
			'1. Error: ' + Trans( laErrs[lnErr, 1] ) + CRLF ;
			+ '2. Message: "' + laErrs[lnErr, 2] + '"' + CRLF ;
			+ '3. ' + Iif( llOleErr Or llOdbcErr, "OLE/ODBC: ", "EMP: " ) + Transform( laErrs[lnErr, 3] ) + CRLF ;
			+ '4. ' + Iif( llOleErr, "App: ", ;
			iif( llOdbcErr, "SQL State: ", ;
			"Work Area (?): " ) + Transform( laErrs[lnErr, 4] ) ) + CRLF ;
			+ '5. ' + Iif( llOleErr, "OLE Help file: " + Trans( laErrs[lnErr, 5] ), ;
			+ Iif( llOdbcErr, "ODBC Error: " + Transform( laErrs[lnErr, 5] ), ;
			+ Iif( llTrigErr, "Trigger: " + Substr( "InsertUpdateDelete", laErrs[lnErr, 5]*6-5, 6 ), ;
			+ "na: " + Transform( laErrs[lnErr, 5] ) ) ) ) + CRLF ;
			+ '6. ' + Iif( llOleErr, "OLE Help Context Id : ", ;
			+ Iif( llOdbcErr, "ODBC Connection Handle: ", ;
			+ "na: " ) ) + Transform( laErrs[lnErr, 6] ) + CRLF ;
			+ '7. ' + Iif( llOleErr, "OLE exception number: ", ;
			+ "na: " ) + Transform( laErrs[lnErr, 6] )

		Do Case
			Case laErrs(1) = 1539
				lcErrMsg = lcErrMsg ;
					+ "Trigger error #: " ;
					+ Transform( laErrs(5) ) + ', ' ;
					+ Substr( "InsertUpdateDelete", laErrs(5)*6-5, 6 ) ;
					+ CR_LF + CR_LF

				If Vartype( gaErrors[1] ) = 'N'
					For lnErrorRow = 1 To Alen( gaErrors, 1 )
						lcErrMsg = lcErrMsg +;
							"1. Error number: " + Trans( gaErrors[lnErrorRow,1] ) + CR_LF + ;
							"2. Error text: " + Trans( gaErrors[lnErrorRow,2] ) + CR_LF + ;
							"3. Source: " + gaErrors[lnErrorRow,3] + CR_LF + ;
							"4. Call stack: " + gaErrors[lnErrorRow,4] + CR_LF + ;
							"5. Parent table name: " + gaErrors[lnErrorRow,5] + CR_LF + ;
							"6. Parent record number: " + Trans( gaErrors[lnErrorRow,6] ) + CR_LF + ;
							"7. Original value of Primary Key of the Parent: " + Trans( gaErrors[lnErrorRow,7] ) + CR_LF + ;
							"8. Parent Primary Key (expression): " + gaErrors[lnErrorRow,8] + CR_LF + ;
							"9. Child Table Name: " + gaErrors[lnErrorRow,9] + CR_LF + ;
							"10. Child Record Number: " + Trans( gaErrors[lnErrorRow,10] ) + CR_LF + ;
							"11. Original value of Foreign Key in the Child: " + Trans( gaErrors[lnErrorRow,11] ) + CR_LF + ;
							"12. Child Foreign Key (expression): " + gaErrors[lnErrorRow,12 ] + CR_LF + CR_LF
					Endfor
				Endif
		Endcase

		? lcErrMsg
		_Cliptext = _Cliptext + CR_LF + lcErrMsg
	Endfor

	Return
Endfunc

Contributors Carl Karsten
Category Error Handling
( Topic last updated: 2003.03.29 09:37:03 AM )