You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2KB

  1. #pragma once
  2. #include <spdlog/fmt/fmt.h>
  3. #include <stdexcept>
  4. #include <string_view>
  5. namespace dds {
  6. struct exception_base : std::runtime_error {
  7. using runtime_error::runtime_error;
  8. };
  9. struct user_error_base : exception_base {
  10. using exception_base::exception_base;
  11. virtual std::string error_reference() const noexcept = 0;
  12. virtual std::string_view explanation() const noexcept = 0;
  13. };
  14. enum class errc {
  15. none = 0,
  16. invalid_builtin_toolchain,
  17. no_such_catalog_package,
  18. };
  19. std::string error_reference_of(errc) noexcept;
  20. std::string_view explanation_of(errc) noexcept;
  21. template <errc ErrorCode>
  22. struct user_error : user_error_base {
  23. using user_error_base::user_error_base;
  24. std::string error_reference() const noexcept override { return error_reference_of(ErrorCode); }
  25. std::string_view explanation() const noexcept override { return explanation_of(ErrorCode); }
  26. };
  27. using error_invalid_default_toolchain = user_error<errc::invalid_builtin_toolchain>;
  28. template <errc ErrorCode, typename... Args>
  29. [[noreturn]] void throw_user_error(std::string_view fmt_str, Args&&... args) {
  30. throw user_error<ErrorCode>(fmt::format(fmt_str, std::forward<Args>(args)...));
  31. }
  32. } // namespace dds