Exception.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2014 Robin Appelman <robin@icewind.nl>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. namespace Icewind\SMB\Exception;
  7. use Throwable;
  8. /**
  9. * @psalm-consistent-constructor
  10. */
  11. class Exception extends \Exception {
  12. public function __construct(string $message = "", int $code = 0, Throwable $previous = null) {
  13. parent::__construct($message, $code, $previous);
  14. }
  15. /**
  16. * @param string|null $path
  17. * @param string|int|null $error
  18. * @return Exception
  19. */
  20. public static function unknown(?string $path, $error): Exception {
  21. $message = 'Unknown error (' . (string)$error . ')';
  22. if ($path) {
  23. $message .= ' for ' . $path;
  24. }
  25. return new Exception($message, is_int($error) ? $error : 0);
  26. }
  27. /**
  28. * @param array<int|string, class-string<Exception>> $exceptionMap
  29. * @param string|int|null $error
  30. * @param string|null $path
  31. * @return Exception
  32. */
  33. public static function fromMap(array $exceptionMap, $error, ?string $path): Exception {
  34. if (isset($exceptionMap[$error])) {
  35. $exceptionClass = $exceptionMap[$error];
  36. if (is_numeric($error)) {
  37. return new $exceptionClass($path, $error);
  38. } else {
  39. return new $exceptionClass($path);
  40. }
  41. } else {
  42. return Exception::unknown($path, $error);
  43. }
  44. }
  45. }