Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
DefaultFaultInterceptor.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
8#include "Stroika/Foundation/IO/Network/HTTP/Exception.h"
10
11#include "DefaultFaultInterceptor.h"
12
13using namespace Stroika::Foundation;
16using namespace Stroika::Foundation::Memory;
17
18using namespace Stroika::Frameworks;
19using namespace Stroika::Frameworks::WebServer;
20
21/*
22 ********************************************************************************
23 ************************* WebServer::Router::Rep_ ******************************
24 ********************************************************************************
25 */
26struct DefaultFaultInterceptor::Rep_ : Interceptor::_IRep {
27 Rep_ () = default;
28 virtual void HandleFault (Message& m, const exception_ptr& e) const noexcept override
29 {
30 Response& response = m.rwResponse ();
31 try {
32 try {
33 rethrow_exception (e);
34 }
35 catch (const IO::Network::HTTP::Exception& ee) {
36 if (response.responseStatusSent) {
37 DbgTrace ("response failed after sending the status: {}"_f, current_exception ()); // else horse has left the barn
38 response.Abort ();
39 }
40 else {
41 response.statusAndOverrideReason = make_tuple (ee.GetStatus (), ee.GetReason ());
42 response.contentType = DataExchange::InternetMediaTypes::kText_PLAIN;
43 response.writeln (Characters::ToString (ee));
44 }
45 }
46 catch (...) {
47 if (response.responseStatusSent) {
48 DbgTrace ("response failed after sending the status: {}"_f, current_exception ()); // else horse has left the barn
49 response.Abort ();
50 }
51 else {
52 response.status = IO::Network::HTTP::StatusCodes::kInternalError;
53 response.contentType = DataExchange::InternetMediaTypes::kText_PLAIN;
54 response.writeln (Characters::ToString (e));
55 }
56 }
57 }
58 catch (...) {
59 DbgTrace (L"Oops! - not good, but nothing todo but burry it: {}"_f, current_exception ()); // else horse has left the barn
60 response.Abort ();
61 }
62 }
63 virtual void HandleMessage ([[maybe_unused]] Message& m) const override
64 {
65 }
66 virtual String ToString () const override
67 {
68 return "DefaultFaultInterceptor"sv;
69 }
70};
71
72struct DefaultFaultInterceptor::Rep_Explicit_ : Interceptor::_IRep {
73 function<void (Message&, const exception_ptr&)> fHandleFault_;
74 Rep_Explicit_ (const function<void (Message&, const exception_ptr&)>& handleFault)
75 : fHandleFault_{handleFault}
76 {
77 }
78 virtual void HandleFault (Message& m, const exception_ptr& e) const noexcept override
79 {
80 fHandleFault_ (m, e);
81 }
82 virtual void HandleMessage ([[maybe_unused]] Message& m) const override
83 {
84 }
85 virtual String ToString () const override
86 {
87 return "DefaultFaultInterceptor(user-provided)"sv;
88 }
89};
90
91/*
92 ********************************************************************************
93 ****************** WebServer::DefaultFaultInterceptor **************************
94 ********************************************************************************
95 */
96DefaultFaultInterceptor::DefaultFaultInterceptor ()
97 : inherited{MakeSharedPtr<Rep_> ()}
98{
99}
100DefaultFaultInterceptor::DefaultFaultInterceptor (const function<void (Message&, const exception_ptr&)>& handleFault)
101 : inherited{MakeSharedPtr<Rep_Explicit_> (handleFault)}
102{
103}
104DefaultFaultInterceptor::DefaultFaultInterceptor (const function<void (Message*, const exception_ptr&)>& handleFault)
105 : DefaultFaultInterceptor{[=] (Message& m, const exception_ptr& e) { handleFault (&m, e); }}
106{
107}
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...
#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