Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DefaultFaultInterceptor.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
8#include "Stroika/Foundation/IO/Network/HTTP/Exception.h"
9
10#include "DefaultFaultInterceptor.h"
11
12using namespace Stroika::Foundation;
15using namespace Stroika::Foundation::Memory;
16
17using namespace Stroika::Frameworks;
18using namespace Stroika::Frameworks::WebServer;
19
20/*
21 ********************************************************************************
22 ************************* WebServer::Router::Rep_ ******************************
23 ********************************************************************************
24 */
25struct DefaultFaultInterceptor::Rep_ : Interceptor::_IRep {
26 Rep_ () = default;
27 virtual void HandleFault (Message& m, const exception_ptr& e) const noexcept override
28 {
29 Response& response = m.rwResponse ();
30 try {
31 try {
32 rethrow_exception (e);
33 }
34 catch (const IO::Network::HTTP::Exception& ee) {
35 if (response.responseStatusSent) {
36 DbgTrace ("response failed after sending the status: {}"_f, current_exception ()); // else horse has left the barn
37 response.Abort ();
38 }
39 else {
40 response.statusAndOverrideReason = make_tuple (ee.GetStatus (), ee.GetReason ());
41 response.contentType = DataExchange::InternetMediaTypes::kText_PLAIN;
42 response.writeln (Characters::ToString (ee));
43 }
44 }
45 catch (...) {
46 if (response.responseStatusSent) {
47 DbgTrace ("response failed after sending the status: {}"_f, current_exception ()); // else horse has left the barn
48 response.Abort ();
49 }
50 else {
51 response.status = IO::Network::HTTP::StatusCodes::kInternalError;
52 response.contentType = DataExchange::InternetMediaTypes::kText_PLAIN;
53 response.writeln (Characters::ToString (e));
54 }
55 }
56 }
57 catch (...) {
58 DbgTrace (L"Oops! - not good, but nothing todo but burry it: {}"_f, current_exception ()); // else horse has left the barn
59 response.Abort ();
60 }
61 }
62 virtual void HandleMessage ([[maybe_unused]] Message& m) const override
63 {
64 }
65 virtual String ToString () const override
66 {
67 return "DefaultFaultInterceptor"sv;
68 }
69};
70
71struct DefaultFaultInterceptor::Rep_Explicit_ : Interceptor::_IRep {
72 function<void (Message&, const exception_ptr&)> fHandleFault_;
73 Rep_Explicit_ (const function<void (Message&, const exception_ptr&)>& handleFault)
74 : fHandleFault_{handleFault}
75 {
76 }
77 virtual void HandleFault (Message& m, const exception_ptr& e) const noexcept override
78 {
79 fHandleFault_ (m, e);
80 }
81 virtual void HandleMessage ([[maybe_unused]] Message& m) const override
82 {
83 }
84 virtual String ToString () const override
85 {
86 return "DefaultFaultInterceptor(user-provided)"sv;
87 }
88};
89
90/*
91 ********************************************************************************
92 ****************** WebServer::DefaultFaultInterceptor **************************
93 ********************************************************************************
94 */
95DefaultFaultInterceptor::DefaultFaultInterceptor ()
96 : inherited{make_shared<Rep_> ()}
97{
98}
99DefaultFaultInterceptor::DefaultFaultInterceptor (const function<void (Message&, const exception_ptr&)>& handleFault)
100 : inherited{make_shared<Rep_Explicit_> (handleFault)}
101{
102}
103DefaultFaultInterceptor::DefaultFaultInterceptor (const function<void (Message*, const exception_ptr&)>& handleFault)
104 : DefaultFaultInterceptor{[=] (Message& m, const exception_ptr& e) { handleFault (&m, e); }}
105{
106}
#define DbgTrace
Definition Trace.h:309
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
virtual void HandleMessage(Message &m) const =0
virtual Characters::String ToString() const
virtual void HandleFault(Message &m, const exception_ptr &e) const noexcept
String ToString(T &&t, ARGS... args)
Return a debug-friendly, display version of the argument: not guaranteed parsable or usable except fo...
Definition ToString.inl:465