2011-04-20 16:40:40 +02: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)
|
|
|
|
*/
|
2012-09-17 07:42:29 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|
2011-04-20 16:40:40 +02:00
|
|
|
|
|
|
|
#include "catch_interfaces_exception.h"
|
2014-04-23 07:51:58 +02:00
|
|
|
#include "catch_tostring.h"
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2012-05-21 22:51:16 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
#import "Foundation/Foundation.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
|
2012-08-06 21:16:53 +02:00
|
|
|
public:
|
2012-05-16 09:02:20 +02:00
|
|
|
~ExceptionTranslatorRegistry() {
|
2012-02-18 20:14:09 +01:00
|
|
|
deleteAll( m_translators );
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
|
2012-08-06 21:16:53 +02:00
|
|
|
virtual void registerTranslator( const IExceptionTranslator* translator ) {
|
2011-04-20 16:40:40 +02:00
|
|
|
m_translators.push_back( translator );
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
virtual std::string translateActiveException() const {
|
|
|
|
try {
|
2012-05-21 22:51:16 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
// In Objective-C try objective-c exceptions first
|
|
|
|
@try {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
return toString( [exception description] );
|
|
|
|
}
|
|
|
|
#else
|
2012-02-17 20:50:59 +01:00
|
|
|
throw;
|
2012-05-21 22:51:16 +02:00
|
|
|
#endif
|
2012-02-17 20:50:59 +01:00
|
|
|
}
|
2014-05-28 19:53:01 +02:00
|
|
|
catch( TestFailureException& ) {
|
|
|
|
throw;
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
catch( std::exception& ex ) {
|
2012-02-17 20:50:59 +01:00
|
|
|
return ex.what();
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
catch( std::string& msg ) {
|
2012-02-17 20:50:59 +01:00
|
|
|
return msg;
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
catch( const char* msg ) {
|
2012-02-17 20:50:59 +01:00
|
|
|
return msg;
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
catch(...) {
|
2012-02-17 20:50:59 +01:00
|
|
|
return tryTranslators( m_translators.begin() );
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-08-06 21:16:53 +02:00
|
|
|
std::string tryTranslators( std::vector<const IExceptionTranslator*>::const_iterator it ) const {
|
2011-04-20 16:40:40 +02:00
|
|
|
if( it == m_translators.end() )
|
|
|
|
return "Unknown exception";
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-16 09:02:20 +02:00
|
|
|
try {
|
2011-04-20 16:40:40 +02:00
|
|
|
return (*it)->translate();
|
|
|
|
}
|
2012-05-16 09:02:20 +02:00
|
|
|
catch(...) {
|
2011-04-20 16:40:40 +02:00
|
|
|
return tryTranslators( it+1 );
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2011-04-20 16:40:40 +02:00
|
|
|
private:
|
2012-08-06 21:16:53 +02:00
|
|
|
std::vector<const IExceptionTranslator*> m_translators;
|
2011-04-20 16:40:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:42:29 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
|