HintException.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCP;
  7. /**
  8. * Class HintException
  9. *
  10. * An Exception class with the intention to be presented to the end user
  11. *
  12. * @package OCP
  13. * @since 23.0.0
  14. */
  15. class HintException extends \Exception {
  16. private $hint;
  17. /**
  18. * HintException constructor.
  19. *
  20. * @since 23.0.0
  21. * @param string $message The error message. It will be not revealed to the
  22. * the user (unless the hint is empty) and thus
  23. * should be not translated.
  24. * @param string $hint A useful message that is presented to the end
  25. * user. It should be translated, but must not
  26. * contain sensitive data.
  27. * @param int $code
  28. * @param \Exception|null $previous
  29. */
  30. public function __construct($message, $hint = '', $code = 0, ?\Exception $previous = null) {
  31. $this->hint = $hint;
  32. parent::__construct($message, $code, $previous);
  33. }
  34. /**
  35. * Returns a string representation of this Exception that includes the error
  36. * code, the message and the hint.
  37. *
  38. * @since 23.0.0
  39. * @return string
  40. */
  41. public function __toString(): string {
  42. return self::class . ": [{$this->code}]: {$this->message} ({$this->hint})\n";
  43. }
  44. /**
  45. * Returns the hint with the intention to be presented to the end user. If
  46. * an empty hint was specified upon instantiation, the message is returned
  47. * instead.
  48. *
  49. * @since 23.0.0
  50. * @return string
  51. */
  52. public function getHint(): string {
  53. if (empty($this->hint)) {
  54. return $this->message;
  55. }
  56. return $this->hint;
  57. }
  58. }