Archive: Error Code detection


Error Code detection
Hi,

I'm trying to capture any errors my applications may come across, but after searching the forums etc I'm still can't see an explanation.

I can see an error if I do something like:

ClearErrors
<try to create file x>
${If} ${Errors}
DetailPrint "Error creating file x"
${EndIf}

It seems like I can only get an "errorFlag" raised and that I have to know the context of the error; i.e I was trying to create "file x" and it failed.

I'd like to do something like:

ClearErrors
<try to create file x>
<try to create file y>
<try to create file z>
${If} ${Errors}
DetailPrint "Error creating file <name of file>"
${EndIf}

...but I think I may have to write:

ClearErrors
<try to create file x>
${If} ${Errors}
DetailPrint "Error creating file x
${EndIf}
ClearErrors
<try to create file y>
${If} ${Errors}
DetailPrint "Error creating file y"
${EndIf}
ClearErrors
<try to create file z>
${If} ${Errors}
DetailPrint "Error creating file z"
${EndIf}

Is this correct, or am I missing something? Thanks.


It's mandatory to ClearErrors just before you're going to perform an action that might set the error flag.


You can wrap it in a macro for easier coding.

!macro FileWithTest file
ClearErrors
File "${file}"
${If} ${Errors}
DetailPrint "error with ${file}"
${EndIf}
!macroend

Thanks guys. I'd realised I could wrap checks into macros, but I thought there may be a way to be more intelligent by say checking errors status codes returned by each command.

It just seems that it could make the code very bulky if I have to check the status of most of the commands I run.


Thanks guys. I'd realised I could wrap checks into macros, but I thought there may be a way to be more intelligent by say checking errors status codes returned by each command.
So where is the difference if instead of:
ClearErrors
File <try to create file x>
IfErrors do_this do_that

you were using:
File <try to create file x> $0 ;return var
StrCmp $0 "<errorlevel> do_this do_that

It doesn't seem a difference to me :)

Sorry red wine, I don't think I've explained myself well enough at all!

If I was writing a Unix script I'd be able to access the return code in the "$?" variable - this may be say "2" for "no such file", or "13" for "permission denied" etc. You could pass that info back to the user so they knew the cause of the error.

Unless I'm missing it, I only seem to be able to get a "you had an error" here. I think this means that I have to wrap each call with a "clearErrors/IfError" type block in order to offer a little more info; eg

ClearErrors
<open file for writing>
${If} ${Errors}
DetailPrint "Cannot open file for writing"
${Else}
ClearErrors
<try to write>
${If} ${Errors}
DetailPrint "Cannot write to file"
${Endif}
${Endif}

Does that make sense?


You're right I guess. I misunderstood.
I thought we're talking about file extraction, indeed regarding to file extraction doesn't make any noticeable difference.