catch_exception_translator_registry.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Created by Phil on 20/04/2011.
  3. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch_exception_translator_registry.h"
  9. #include "catch_assertionhandler.h"
  10. #include "catch_compiler_capabilities.h"
  11. #include "catch_enforce.h"
  12. #ifdef __OBJC__
  13. #import "Foundation/Foundation.h"
  14. #endif
  15. namespace Catch {
  16. ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() {
  17. }
  18. void ExceptionTranslatorRegistry::registerTranslator( const IExceptionTranslator* translator ) {
  19. m_translators.push_back( std::unique_ptr<const IExceptionTranslator>( translator ) );
  20. }
  21. #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  22. std::string ExceptionTranslatorRegistry::translateActiveException() const {
  23. try {
  24. #ifdef __OBJC__
  25. // In Objective-C try objective-c exceptions first
  26. @try {
  27. return tryTranslators();
  28. }
  29. @catch (NSException *exception) {
  30. return Catch::Detail::stringify( [exception description] );
  31. }
  32. #else
  33. // Compiling a mixed mode project with MSVC means that CLR
  34. // exceptions will be caught in (...) as well. However, these
  35. // do not fill-in std::current_exception and thus lead to crash
  36. // when attempting rethrow.
  37. // /EHa switch also causes structured exceptions to be caught
  38. // here, but they fill-in current_exception properly, so
  39. // at worst the output should be a little weird, instead of
  40. // causing a crash.
  41. if (std::current_exception() == nullptr) {
  42. return "Non C++ exception. Possibly a CLR exception.";
  43. }
  44. return tryTranslators();
  45. #endif
  46. }
  47. catch( TestFailureException& ) {
  48. std::rethrow_exception(std::current_exception());
  49. }
  50. catch( std::exception& ex ) {
  51. return ex.what();
  52. }
  53. catch( std::string& msg ) {
  54. return msg;
  55. }
  56. catch( const char* msg ) {
  57. return msg;
  58. }
  59. catch(...) {
  60. return "Unknown exception";
  61. }
  62. }
  63. std::string ExceptionTranslatorRegistry::tryTranslators() const {
  64. if (m_translators.empty()) {
  65. std::rethrow_exception(std::current_exception());
  66. } else {
  67. return m_translators[0]->translate(m_translators.begin() + 1, m_translators.end());
  68. }
  69. }
  70. #else // ^^ Exceptions are enabled // Exceptions are disabled vv
  71. std::string ExceptionTranslatorRegistry::translateActiveException() const {
  72. CATCH_INTERNAL_ERROR("Attempted to translate active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
  73. }
  74. std::string ExceptionTranslatorRegistry::tryTranslators() const {
  75. CATCH_INTERNAL_ERROR("Attempted to use exception translators under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
  76. }
  77. #endif
  78. }