]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TextException.cc
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "base/TextException.h"
11 #include "sbuf/SBuf.h"
15 #include <unordered_map>
17 /// a standard CoW string; avoids noise and circular dependencies of SBuf
18 typedef std::runtime_error WhatString
;
20 /// a collection of strings indexed by pointers to their creator objects
21 typedef std::unordered_multimap
<const void*, WhatString
> WhatStrings
;
23 /// requested what() strings of alive TextException objects
24 static WhatStrings
*WhatStrings_
= nullptr;
26 TextException::TextException(SBuf message
, const SourceLocation
&location
):
27 TextException(message
.c_str(), location
)
30 TextException::~TextException() throw()
33 WhatStrings_
->erase(this); // there only if what() has been called
37 TextException::print(std::ostream
&os
) const
39 os
<< std::runtime_error::what() <<
40 Debug::Extra
<< "exception location: " << where
;
41 // TODO: ...error_detail: " << (ERR_DETAIL_EXCEPTION_START+id());
46 TextException::what() const throw()
48 std::ostringstream os
;
50 const WhatString
result(os
.str());
52 // extend result.c_str() lifetime to this object lifetime
54 WhatStrings_
= new WhatStrings
;
55 // *this could change, but we must preserve old results for they may be used
56 WhatStrings_
->emplace(std::make_pair(this, result
));
62 operator <<(std::ostream
&os
, const TextException
&ex
)
69 CurrentException(std::ostream
&os
)
71 if (std::current_exception()) {
73 throw; // re-throw to recognize the exception type
75 catch (const TextException
&ex
) {
76 os
<< ex
; // optimization: this is a lot cheaper than what() below
78 catch (const std::exception
&ex
) {
82 os
<< "[unknown exception type]";
85 os
<< "[no active exception]";