Perforce Public Knowledge Base - P4 Client Error Handling
Reset Search
 

 

Article

P4 Client Error Handling

« Go Back

Information

 
Problem
This article describes Perforce command return values and some of the ways they are generated. Commands can fail in different ways and not all "failures" generate a non-zero exit code. You can use the global -s flag to examine Perforce exit status.
 
Solution
The Perforce Command Line Client only exits with a non-zero return value when the p4 command itself fails. For example, a p4 command fails when a bogus option argument is used:
 
 dhcp36$ p4 -s client -badflag
 error: Usage: client [ -d -f -i -o -t template ] [ clientname ]
 error: Invalid option: -badflag.
 exit: 1

There are variety of other error conditions for example, file(s) not in client view, that generate info or warning messages.   These error conditions do not cause the command to fail and exit with a non-zero status. The command might not produce the expected or desired result, but if it ran successfully to completion, it will exit 0 while still generating an error message.

To see how Perforce treats various error conditions, use the global -s flag for scripting. For example, if you sync a non-existent file, you get an error message, but the command still exits with a return value of zero:
 
 C:\p4work\dev>p4 -s sync junk
 error: junk - no such file(s).
 exit: 0

If you do not have permissions to view the given file argument, you see a different message; again, the command exits with a return value of zero:

 C:\p4work\dev>p4 -s sync junk
 error: junk - protected namespace - access denied.
 exit: 0

A successful sync generates an "info" message and also exits with a return value of zero:
 C:\p4work\dev\py>p4 -s sync wc.py
 info: //depot/dev/py/wc.py#1 - added as c:\p4work\dev\py\wc.py
 exit: 0
 
Recommendations

In general, you should not rely on a command exiting non-zero on "failure". Instead, use the global -s flag to help interpret the command output -- and parse that output in your scripts. Possible tags with the -s flag include info:, info1:, info2:, error:, and text:.  The error: tag appears when Perforce considers the message to be an error or warning. The text: tag appears for a few commands, for example, on the diff output from the p4 describe command. Since it might be difficult to know all of the possible error messages, it is often easiest to look for info: messages that indicate success.  Be sure to test the commands you intend to use in order to be sure that you are looking for the correct info tag.
 
Below are some common messages for which a well-crafted script might account.
 
Connection errors
Each of these connection errors can be caught with the exit code, but you might be able to recover if you know the message.
Connect to server failed; check $P4PORT.

Your session has expired, please login again.

Perforce password (P4PASSWD) invalid or unset.
 
File operations
These are common messages for file operations. The "File(s) up-to-date" message is returned on a sync operation when the client workspace is already synced, but registers as an "error:" nonetheless.
no such file(s).

file(s) up-to-date.

Request too large (over <maxResults>); see 'p4 help maxresults'.

Too many rows scanned (over <maxScanRows>); see 'p4 help maxscanrows'.
 
Changelist operations
no such changelist.

file(s) not opened in that changelist.

no file(s) at that changelist number.

See p4 help usage for more information on Perforce global flags.
Related Links

Feedback

 

Was this article helpful?


   

Feedback

Please tell us how we can make this article more useful.

Characters Remaining: 255