-#include "config.h"
+/*
+ * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
#include "base/TextException.h"
-#include "Debug.h"
-#include "util.h"
+#include "sbuf/SBuf.h"
-TextException::TextException()
-{
- message=NULL;
- theFileName=NULL;
- theLineNo=0;
-}
+#include <iostream>
+#include <sstream>
+#include <unordered_map>
-TextException::TextException(const TextException& right) :
- message((right.message?xstrdup(right.message):NULL)), theFileName(right.theFileName), theLineNo(right.theLineNo)
-{
-}
+/// a standard CoW string; avoids noise and circular dependencies of SBuf
+typedef std::runtime_error WhatString;
+
+/// a collection of strings indexed by pointers to their creator objects
+typedef std::unordered_multimap<const void*, WhatString> WhatStrings;
+
+/// requested what() strings of alive TextException objects
+static WhatStrings *WhatStrings_ = nullptr;
-TextException::TextException(const char *aMsg, const char *aFileName, int aLineNo):
- message(xstrdup(aMsg)), theFileName(aFileName), theLineNo(aLineNo)
+TextException::TextException(SBuf message, const SourceLocation &location):
+ TextException(message.c_str(), location)
{}
TextException::~TextException() throw()
{
- if (message) xfree(message);
+ if (WhatStrings_)
+ WhatStrings_->erase(this); // there only if what() has been called
}
-TextException& TextException::operator=(const TextException &right)
+std::ostream &
+TextException::print(std::ostream &os) const
{
- if (this==&right) return *this;
- if (message) xfree(message);
- message=(right.message?xstrdup(right.message):NULL);
- theFileName=right.theFileName;
- theLineNo=right.theLineNo;
-
- return *this;
+ os << std::runtime_error::what() <<
+ Debug::Extra << "exception location: " << where;
+ // TODO: ...error_detail: " << (ERR_DETAIL_EXCEPTION_START+id());
+ return os;
}
-const char *TextException::what() const throw()
+const char *
+TextException::what() const throw()
{
- /// \todo add file:lineno
- return message ? message : "TextException without a message";
-}
+ std::ostringstream os;
+ print(os);
+ const WhatString result(os.str());
-void Throw(const char *message, const char *fileName, int lineNo)
-{
+ // extend result.c_str() lifetime to this object lifetime
+ if (!WhatStrings_)
+ WhatStrings_ = new WhatStrings;
+ // *this could change, but we must preserve old results for they may be used
+ WhatStrings_->emplace(std::make_pair(this, result));
- // or should we let the exception recepient print the exception instead?
+ return result.what();
+}
- if (fileName) {
- debugs(0, 3, fileName << ':' << lineNo << ": exception" <<
- (message ? ": " : ".") << (message ? message : ""));
+std::ostream &
+CurrentException(std::ostream &os)
+{
+ if (std::current_exception()) {
+ try {
+ throw; // re-throw to recognize the exception type
+ }
+ catch (const std::exception &ex) {
+ os << ex.what();
+ }
+ catch (...) {
+ os << "[unknown exception type]";
+ }
} else {
- debugs(0, 3, "exception" <<
- (message ? ": " : ".") << (message ? message : ""));
+ os << "[no active exception]";
}
-
- throw TextException(message, fileName, lineNo);
+ return os;
}
+