Skip to content

Commit

Permalink
#5108: Streamline exception handling a bit, reducing the amount of ca…
Browse files Browse the repository at this point in the history
…tch blocks
  • Loading branch information
codereader committed Nov 28, 2020
1 parent e56d7a7 commit db49aa7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
28 changes: 23 additions & 5 deletions include/imapresource.h
Expand Up @@ -30,14 +30,32 @@ class IMapResource
virtual bool load() = 0;

// Exception type thrown by the the MapResource implementation
struct OperationException :
class OperationException :
public std::runtime_error
{
private:
bool _cancelled;

public:
OperationException(const std::string& msg) :
runtime_error(msg)
{
rError() << "MapResource operation failed: " << msg << std::endl;
}
OperationException(msg, false)
{}

OperationException(const std::string& msg, bool cancelled) :
runtime_error(msg),
_cancelled(cancelled)
{
if (!_cancelled)
{
rError() << "MapResource operation failed: " << msg << std::endl;
}
}

// Returns true if the operation has been cancelled by the user
bool operationCancelled() const
{
return _cancelled;
}
};

/**
Expand Down
10 changes: 3 additions & 7 deletions radiantcore/map/MapResource.cpp
Expand Up @@ -304,15 +304,11 @@ RootNodePtr MapResource::loadMapNode()
}
}
}
catch (FileOperation::OperationCancelled&)
{
// User cancelled, don't show a complicated failure message
throw OperationException(_("Map loading cancelled"));
}
catch (const OperationException& ex)
{
// Re-throw the exception, adding the map file path to the message
throw OperationException(fmt::format(_("Failure reading map file:\n{0}\n\n{1}"), fullpath, ex.what()));
// Re-throw the exception, prepending the map file path to the message (if not cancelled)
throw ex.operationCancelled() ? ex :
OperationException(fmt::format(_("Failure reading map file:\n{0}\n\n{1}"), fullpath, ex.what()));
}

return rootNode;
Expand Down
2 changes: 1 addition & 1 deletion radiantcore/map/MapResourceLoader.cpp
Expand Up @@ -46,7 +46,7 @@ RootNodePtr MapResourceLoader::load()
scene::NodeRemover remover;
root->traverseChildren(remover);

throw; // leak this exception
throw IMapResource::OperationException(_("Map loading cancelled"), true); // cancelled flag set
}
catch (IMapReader::FailureException& e)
{
Expand Down
4 changes: 1 addition & 3 deletions radiantcore/map/MapResourceLoader.h
Expand Up @@ -30,9 +30,7 @@ class MapResourceLoader

// Process the stream passed to the constructor, returns
// the root node
// Throws exceptions on failure:
// - FileOperation::OperationCancelled in case the user cancelled
// - IMapResource::OperationException in other cases
// Throws IMapResource::OperationException on failure or cancel
RootNodePtr load();

// Load the info file from the given stream, apply it to the root node
Expand Down

0 comments on commit db49aa7

Please sign in to comment.