Files
Catch2/include/internal/catch_exception_translator_registry.hpp
T

76 lines
2.1 KiB
C++
Raw Normal View History

2011-04-20 15:40:40 +01:00
/*
* Created by Phil on 20/04/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
2011-04-20 15:40:40 +01:00
#include "catch_interfaces_exception.h"
2013-09-14 19:58:45 +01:00
#include "catch_tostring.hpp"
2011-04-20 15:40:40 +01:00
2012-05-21 21:51:16 +01:00
#ifdef __OBJC__
#import "Foundation/Foundation.h"
#endif
2012-05-16 08:02:20 +01:00
namespace Catch {
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
public:
2012-05-16 08:02:20 +01:00
~ExceptionTranslatorRegistry() {
2012-02-18 19:14:09 +00:00
deleteAll( m_translators );
}
2012-05-16 08:02:20 +01:00
virtual void registerTranslator( const IExceptionTranslator* translator ) {
2011-04-20 15:40:40 +01:00
m_translators.push_back( translator );
}
2013-07-03 19:14:59 +01:00
2012-05-16 08:02:20 +01:00
virtual std::string translateActiveException() const {
try {
2012-05-21 21:51:16 +01:00
#ifdef __OBJC__
// In Objective-C try objective-c exceptions first
@try {
throw;
}
@catch (NSException *exception) {
return toString( [exception description] );
}
#else
2012-02-17 19:50:59 +00:00
throw;
2012-05-21 21:51:16 +01:00
#endif
2012-02-17 19:50:59 +00:00
}
2012-05-16 08:02:20 +01:00
catch( std::exception& ex ) {
2012-02-17 19:50:59 +00:00
return ex.what();
}
2012-05-16 08:02:20 +01:00
catch( std::string& msg ) {
2012-02-17 19:50:59 +00:00
return msg;
}
2012-05-16 08:02:20 +01:00
catch( const char* msg ) {
2012-02-17 19:50:59 +00:00
return msg;
}
2012-05-16 08:02:20 +01:00
catch(...) {
2012-02-17 19:50:59 +00:00
return tryTranslators( m_translators.begin() );
}
2011-04-20 15:40:40 +01:00
}
2013-07-03 19:14:59 +01:00
std::string tryTranslators( std::vector<const IExceptionTranslator*>::const_iterator it ) const {
2011-04-20 15:40:40 +01:00
if( it == m_translators.end() )
return "Unknown exception";
2013-07-03 19:14:59 +01:00
2012-05-16 08:02:20 +01:00
try {
2011-04-20 15:40:40 +01:00
return (*it)->translate();
}
2012-05-16 08:02:20 +01:00
catch(...) {
2011-04-20 15:40:40 +01:00
return tryTranslators( it+1 );
}
}
2013-07-03 19:14:59 +01:00
2011-04-20 15:40:40 +01:00
private:
std::vector<const IExceptionTranslator*> m_translators;
2011-04-20 15:40:40 +01:00
};
}
#endif // TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED