Take an exception, print information about it and return the exit code to use
(exc: Exception)
| 172 | |
| 173 | |
| 174 | def handle_exception(exc: Exception) -> int: |
| 175 | """ |
| 176 | Take an exception, print information about it and return the exit code to use |
| 177 | """ |
| 178 | # TODO: fix this. It's required to avoid a circular import error |
| 179 | from ggshield.core import ui |
| 180 | |
| 181 | if isinstance(exc, click.exceptions.Abort): |
| 182 | return ExitCode.SUCCESS |
| 183 | |
| 184 | # Get exit code |
| 185 | if isinstance(exc, _ExitError): |
| 186 | exit_code = exc.exit_code |
| 187 | elif isinstance(exc, (InvalidGitRefError, click.UsageError)): |
| 188 | exit_code = ExitCode.USAGE_ERROR |
| 189 | else: |
| 190 | exit_code = ExitCode.UNEXPECTED_ERROR |
| 191 | |
| 192 | click.echo() |
| 193 | ui.display_error(str(exc)) |
| 194 | if isinstance(exc, UnicodeEncodeError) and platform.system() == "Windows": |
| 195 | ui.display_info( |
| 196 | "\n" |
| 197 | "ggshield failed to print a message because of an Unicode encoding issue." |
| 198 | " To workaround that, try setting the PYTHONUTF8 environment variable to 1." |
| 199 | ) |
| 200 | |
| 201 | if not isinstance(exc, click.ClickException): |
| 202 | click.echo() |
| 203 | if ui.is_verbose(): |
| 204 | traceback.print_exc() |
| 205 | else: |
| 206 | ui.display_info("Re-run the command with --verbose to get a stack trace.") |
| 207 | |
| 208 | return exit_code |
| 209 | |
| 210 | |
| 211 | def handle_api_error(detail: Detail) -> None: |
no test coverage detected