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