I kindly need your support to solve a matter that is driving me crazy since a couple of days...
I can't export the variables (that handle the last error) outside of every function in my library.
Code: Select all
@echo off
%~d0
cd %~dp0
SET "Library.TRUE=TRUE"
SET "Library.Error.Occurred="
SET "Library.Error.Code="
SET "Library.Error.Message="
SET "Library.Error.Date="
SET "Library.Error.Time="
SETLOCAL EnableDelayedExpansion
ECHO Calling [ReceiveError]...
CALL:ReceiveError
:: ### STEP 3 ### - Here I should receive the error
ECHO ErrOccurred [%Library.Error.Occurred%]
ECHO ErrMessage [%Library.Error.Message%]
GOTO:EOF
:ReceiveError
SETLOCAL
IF 0 EQU 0 (
:: ### STEP 1 ## - Generate an example error
CALL:GenerateError
IF "!Library.Error.Occurred!" EQU "%Library.TRUE%" (
GOTO MyExit
)
)
REM Do some stuff
REM Do some stuff
REM Do some stuff
:MyExit
(
ENDLOCAL
:: ### STEP 2 ### - Export the error variables outside, bypassing ENDLOCAL barrier
SET "Library.Error.Occurred=%Library.Error.Occurred%"
SET "Library.Error.Code=%Library.Error.Code%"
SET "Library.Error.Message=%Library.Error.Message%"
SET "Library.Error.Date=%Library.Error.Date%"
SET "Library.Error.Time=%Library.Error.Time%"
SET "O_01=0"
)
GOTO:EOF
:GenerateError
SETLOCAL
IF 0 NEQ 1 (
(
ENDLOCAL
CALL:Library.Error.Handle Y, 1, "This is an error example"
GOTO:EOF
)
)
GOTO:EOF
:Library.Error.Handle
:: Description:
:: Handle error from data provided
:: IN: ARGUMENTS
:: %~1: Show to Screen
:: %~2: Error Code
:: %~3: Error Message
SET "Library.Error.Occurred=%Library.TRUE%"
SET "Library.Error.Code=%~2"
SET "Library.Error.Message=%~3"
SET "Library.Error.Date=%DATE%"
SET "Library.Error.Time=%TIME%"
IF "%~1" EQU "Y" (
ECHO ### ERROR ###: Error occurred
ECHO ### ERROR ###: Message: [%Library.Error.Message%]
ECHO ### ERROR ###: Code: [%Library.Error.Code%]
ECHO ### ERROR ###: Date and Time: [%Library.Error.Date% %Library.Error.Time%]
)
GOTO:EOF
What I need is to have a better method to export all the variables I need.
In ### STEP 2 ### I wish to replace:
Code: Select all
SET "Library.Error.Occurred=%Library.Error.Occurred%"
SET "Library.Error.Code=%Library.Error.Code%"
SET "Library.Error.Message=%Library.Error.Message%"
SET "Library.Error.Date=%Library.Error.Date%"
SET "Library.Error.Time=%Library.Error.Time%"
SET "O_01=0"
with a macro / function / something else:
Code: Select all
%SomethingThatExportEveryErrorVariables%
SET "O_01=0"
I prefer to get an "Object" method, calling some routine or macro, because IMHO it would be better for at least these reasons:
1) To have only one instruction, to avoid copy/paste errors or type mistakes (maybe 1 line wrong out of 5);
2) To have a more manageable flow: any modify inside routine / macro in the library will take effect everywhere with zero modifies outside of the library;
3) Improves readability.
What do you suggest?
What's wrong in this code?
Any hint is really appreciated
Thanks in advance
SilverHawk
EDIT: I modified routine ReceiveError and some below code to explain better what's my request. I hope it helps.