2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-09-13 20:56:22 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-07 21:54:50 +00:00
|
|
|
#pragma once
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2016-01-27 17:50:51 +00:00
|
|
|
#include <memory>
|
2022-05-18 13:27:23 +00:00
|
|
|
#include <stdexcept>
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/Assertions.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Pcsx2Defs.h"
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
namespace Exception
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
class BaseException;
|
|
|
|
|
2022-10-12 15:42:36 +00:00
|
|
|
std::unique_ptr<BaseException> FromErrno(std::string streamname, int errcode);
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// BaseException
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// std::exception sucks, and isn't entirely cross-platform reliable in its implementation,
|
|
|
|
// so I made a replacement. The internal messages are non-const, which means that a
|
|
|
|
// catch clause can optionally modify them and then re-throw to a top-level handler.
|
|
|
|
//
|
|
|
|
// Note, this class is "abstract" which means you shouldn't use it directly like, ever.
|
|
|
|
// Use Exception::RuntimeError instead for generic exceptions.
|
|
|
|
//
|
|
|
|
// Because exceptions are the (only!) really useful example of multiple inheritance,
|
|
|
|
// this class has only a trivial constructor, and must be manually initialized using
|
|
|
|
// InitBaseEx() or by individual member assignments. This is because C++ multiple inheritence
|
|
|
|
// is, by design, a lot of fail, especially when class initializers are mixed in.
|
|
|
|
//
|
|
|
|
// [TODO] : Add an InnerException component, and Clone() facility.
|
|
|
|
//
|
|
|
|
class BaseException
|
|
|
|
{
|
|
|
|
protected:
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string m_message_diag; // (untranslated) a "detailed" message of what disastrous thing has occurred!
|
|
|
|
std::string m_message_user; // (translated) a "detailed" message of what disastrous thing has occurred!
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~BaseException() = default;
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
const std::string& DiagMsg() const { return m_message_diag; }
|
|
|
|
const std::string& UserMsg() const { return m_message_user; }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string& DiagMsg() { return m_message_diag; }
|
|
|
|
std::string& UserMsg() { return m_message_user; }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
BaseException& SetBothMsgs(const char* msg_diag);
|
|
|
|
BaseException& SetDiagMsg(std::string msg_diag);
|
|
|
|
BaseException& SetUserMsg(std::string msg_user);
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
// Returns a message suitable for diagnostic / logging purposes.
|
|
|
|
// This message is always in English, and includes a full stack trace.
|
2022-05-18 13:27:23 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
// Returns a message suitable for end-user display.
|
|
|
|
// This message is usually meant for display in a user popup or such.
|
2022-05-18 13:27:23 +00:00
|
|
|
virtual std::string FormatDisplayMessage() const;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
virtual void Rethrow() const = 0;
|
|
|
|
virtual BaseException* Clone() const = 0;
|
|
|
|
};
|
|
|
|
|
2009-09-01 00:43:28 +00:00
|
|
|
// Some helper macros for defining the standard constructors of internationalized constructors
|
|
|
|
// Parameters:
|
|
|
|
// classname - Yeah, the name of this class being defined. :)
|
|
|
|
//
|
|
|
|
// defmsg - default message (in english), which will be used for both english and i18n messages.
|
|
|
|
// The text string will be passed through the translator, so if it's int he gettext database
|
|
|
|
// it will be optionally translated.
|
|
|
|
//
|
2010-05-03 13:51:46 +00:00
|
|
|
// BUGZ?? I'd rather use 'classname' on the Clone() prototype, but for some reason it generates
|
2010-06-28 18:03:54 +00:00
|
|
|
// ambiguity errors on virtual inheritance (it really shouldn't!). So I have to force it to the
|
2010-05-03 13:51:46 +00:00
|
|
|
// BaseException base class. Not sure if this is Stupid Standard Tricks or Stupid MSVC Tricks. --air
|
|
|
|
//
|
|
|
|
// (update: web searches indicate it's MSVC specific -- happens in 2008, not sure about 2010).
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
#define DEFINE_EXCEPTION_COPYTORS(classname, parent) \
|
2021-09-06 18:28:26 +00:00
|
|
|
private: \
|
|
|
|
typedef parent _parent; \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
virtual ~classname() = default; \
|
|
|
|
\
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual void Rethrow() const override \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
|
|
|
throw *this; \
|
|
|
|
} \
|
|
|
|
\
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual classname* Clone() const override \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
|
|
|
return new classname(*this); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFINE_EXCEPTION_MESSAGES(classname) \
|
|
|
|
public: \
|
2022-05-18 13:27:23 +00:00
|
|
|
classname& SetBothMsgs(const char* msg_diag) \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
|
|
|
BaseException::SetBothMsgs(msg_diag); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
\
|
2022-05-18 13:27:23 +00:00
|
|
|
classname& SetDiagMsg(std::string msg_diag) \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
|
|
|
m_message_diag = msg_diag; \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
\
|
2022-05-18 13:27:23 +00:00
|
|
|
classname& SetUserMsg(std::string msg_user) \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
2022-05-18 13:27:23 +00:00
|
|
|
m_message_user = std::move(msg_user); \
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this; \
|
|
|
|
}
|
2016-11-12 15:28:37 +00:00
|
|
|
|
|
|
|
#define DEFINE_RUNTIME_EXCEPTION(classname, parent, message) \
|
2021-09-06 18:28:26 +00:00
|
|
|
DEFINE_EXCEPTION_COPYTORS(classname, parent) \
|
|
|
|
classname() \
|
|
|
|
{ \
|
|
|
|
SetDiagMsg(message); \
|
|
|
|
} \
|
|
|
|
DEFINE_EXCEPTION_MESSAGES(classname)
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------
|
|
|
|
// RuntimeError - Generalized Exceptions with Recoverable Traits!
|
|
|
|
// ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class RuntimeError : public BaseException
|
|
|
|
{
|
|
|
|
DEFINE_EXCEPTION_COPYTORS(RuntimeError, BaseException)
|
|
|
|
DEFINE_EXCEPTION_MESSAGES(RuntimeError)
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool IsSilent;
|
|
|
|
|
|
|
|
RuntimeError() { IsSilent = false; }
|
2022-05-18 13:27:23 +00:00
|
|
|
RuntimeError(const std::runtime_error& ex, const char* prefix = nullptr);
|
|
|
|
RuntimeError(const std::exception& ex, const char* prefix = nullptr);
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------
|
|
|
|
// Streaming (file) Exceptions:
|
|
|
|
// Stream / BadStream / CannotCreateStream / FileNotFound / AccessDenied / EndOfStream
|
|
|
|
// ---------------------------------------------------------------------------------------
|
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
#define DEFINE_STREAM_EXCEPTION(classname, parent) \
|
|
|
|
DEFINE_RUNTIME_EXCEPTION(classname, parent, "") \
|
|
|
|
classname(std::string filename) \
|
|
|
|
{ \
|
|
|
|
StreamName = filename; \
|
|
|
|
} \
|
|
|
|
virtual classname& SetStreamName(std::string name) override \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
2022-05-18 13:27:23 +00:00
|
|
|
StreamName = std::move(name); \
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
\
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual classname& SetStreamName(const char* name) override \
|
2021-09-06 18:28:26 +00:00
|
|
|
{ \
|
2022-05-18 13:27:23 +00:00
|
|
|
StreamName = name; \
|
2021-09-06 18:28:26 +00:00
|
|
|
return *this; \
|
|
|
|
}
|
|
|
|
|
|
|
|
// A generic base error class for bad streams -- corrupted data, sudden closures, loss of
|
|
|
|
// connection, or anything else that would indicate a failure to open a stream or read the
|
|
|
|
// data after the stream was successfully opened.
|
|
|
|
//
|
|
|
|
class BadStream : public RuntimeError
|
|
|
|
{
|
2022-05-24 15:12:56 +00:00
|
|
|
DEFINE_RUNTIME_EXCEPTION(BadStream, RuntimeError, "")
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
public:
|
2022-05-24 15:12:56 +00:00
|
|
|
BadStream(std::string filename)
|
|
|
|
: StreamName(std::move(filename))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual BadStream& SetStreamName(std::string name)
|
|
|
|
{
|
|
|
|
StreamName = std::move(name);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
virtual BadStream& SetStreamName(const char* name)
|
|
|
|
{
|
|
|
|
StreamName = name;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string StreamName; // name of the stream (if applicable)
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const override;
|
|
|
|
virtual std::string FormatDisplayMessage() const override;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
protected:
|
2022-05-18 13:27:23 +00:00
|
|
|
void _formatDiagMsg(std::string& dest) const;
|
|
|
|
void _formatUserMsg(std::string& dest) const;
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A generic exception for odd-ball stream creation errors.
|
|
|
|
//
|
|
|
|
class CannotCreateStream : public BadStream
|
|
|
|
{
|
|
|
|
DEFINE_STREAM_EXCEPTION(CannotCreateStream, BadStream)
|
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const override;
|
|
|
|
virtual std::string FormatDisplayMessage() const override;
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Exception thrown when an attempt to open a non-existent file is made.
|
|
|
|
// (this exception can also mean file permissions are invalid)
|
|
|
|
//
|
|
|
|
class FileNotFound : public CannotCreateStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DEFINE_STREAM_EXCEPTION(FileNotFound, CannotCreateStream)
|
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const override;
|
|
|
|
virtual std::string FormatDisplayMessage() const override;
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AccessDenied : public CannotCreateStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DEFINE_STREAM_EXCEPTION(AccessDenied, CannotCreateStream)
|
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const override;
|
|
|
|
virtual std::string FormatDisplayMessage() const override;
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// EndOfStream can be used either as an error, or used just as a shortcut for manual
|
|
|
|
// feof checks.
|
|
|
|
//
|
|
|
|
class EndOfStream : public BadStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DEFINE_STREAM_EXCEPTION(EndOfStream, BadStream)
|
|
|
|
|
2022-05-24 15:12:56 +00:00
|
|
|
virtual std::string FormatDiagnosticMessage() const override;
|
|
|
|
virtual std::string FormatDisplayMessage() const override;
|
2021-09-06 18:28:26 +00:00
|
|
|
};
|
|
|
|
} // namespace Exception
|
2010-04-27 13:12:03 +00:00
|
|
|
|
|
|
|
using Exception::BaseException;
|