Skip to content

API Controller Development Best Practices for Error Handling

Malintha Amarasinghe edited this page Mar 13, 2020 · 5 revisions
  1. Read through https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46
  2. 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, use utils.Logln or utils.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.
  1. 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)
Clone this wiki locally