Introduction
Like all software, return codes are an essential part of Salt. They are used to identify errors, which helps debug and report failure to the user. For example - the famous 404 response code is a type of return code used by the HTTP protocol. Everyone who uses the Internet will be familiar with its meaning - that the URL they tried to access was not found.

This article will go through the Return Codes used by Salt and their meaning. We will also cover the concept of Returners - what they are, how to use them, and how we can implement our own custom returners.
Return Codes
The concept of return codes is frequently used in the software industry. Return codes are special codes sent along with the main data to convey information about the response.
For example, HTTP uses return codes (they are called response codes, in that case) to convey information about the requested resource (URL). A 200 response code means we found the resource and sent it back, and a 301 means that the resource exists, but it's been moved to a different location. A 404 means that the server couldn't find the resource.
Salt also has similar return codes. Instead of many like HTTP - Salt defaults to only 2 return codes - 0 and 1. 0 is used for correct responses, and 1 when some error has occurred.
1 is returned in the following cases:
-
Errors occur while running states or a state gives back a False output.
-
Any exception is raised while running any code.
-
When running remote commands (like cp.get_file), often the Minion sends back a response in the form of a Dictionary, also called a Map.
If the dictionary has a key result whose value is False, then we return with return code 1.
Similarly, if the dictionary has a key success, with a value False, we return with return code 1.
Retcode
If we are writing custom Salt codes, such as custom states or execution modules, we can customize the error codes returned.
While writing code for State Modules, or Execution Modules, a Python dictionary named __context__ is created by default for us. This is created by the Salt libraries that we import. While states run, this dictionary persists, and all states use the common __context__ dictionary. It is destroyed only after all state runs have finished.
This helps make things efficient. For example, many states may need to fetch files from the Master using the file client module. It would have to be initialized every time. But, using the __context__ dictionary, we can initialize it once and store it in the dictionary. Then each state module can access it from there.
To set custom return codes, we can set the retcode key in __context__.
For example:
if successful==False:
__context__["retcode"] = 10
We can set different error codes for different errors, depending on what we need.









