-
Notifications
You must be signed in to change notification settings - Fork 132
API Controller Development Best Practices for Error Handling
Malintha Amarasinghe edited this page Mar 13, 2020
·
5 revisions
- Read through https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46
- Try to use the below methods as appropriate
-
fmt.Println
: Use only when you are providing an actual output for the command that is running. Do not use this for intermediate info logs. For that, useutils.Logln
orutils.Logf
-
utils.Logln
: Provide verbose logs. Will be printed only when --verbose mode is enabled. -
utils.Logf
: Same as above, except it prints formatted logs
In error cases:
-
utils.HandleErrorAndExit
: When the program cannot continue and it needs to exist after printing the error. -
utils.HandleErrorAndContinue
: When the program can continue but it needs to print the error.
- Mind the streams:
-
stdout
is only for the actual output of the command -
stderr
is for messages (not only error messages but also other info messages)