This is an automated email from the ASF dual-hosted git repository.

cmcfarlen pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 07f66ad00141a96496ca0226c4ebda2e1ecc3112
Author: Leif Hedstrom <[email protected]>
AuthorDate: Mon Mar 17 16:34:44 2025 -0600

    Error::Status can now take optional reason (#12098)
    
    (cherry picked from commit e91c07d13be7ae3c9d16ad36f7ff5019a3a497cd)
---
 doc/developer-guide/cripts/cripts-misc.en.rst | 10 ++++++++--
 include/cripts/Context.hpp                    |  2 +-
 include/cripts/Error.hpp                      |  6 +++---
 src/cripts/Error.cc                           |  7 ++++++-
 4 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/doc/developer-guide/cripts/cripts-misc.en.rst 
b/doc/developer-guide/cripts/cripts-misc.en.rst
index a3e64f73c0..698be215a5 100644
--- a/doc/developer-guide/cripts/cripts-misc.en.rst
+++ b/doc/developer-guide/cripts/cripts-misc.en.rst
@@ -36,8 +36,8 @@ Errors
 ======
 
 Often it's useful to be able to abort a client transaction prematurely, and
-return an error to the client. Cripts provid
-making this easy.
+return an error to the client. Cripts provides a simple way to do this, using
+the ``cripts::Error`` object.
 
 .. note::
     Explicitly forcing an HTTP error overrides any other response status that 
may have been set.
@@ -68,6 +68,12 @@ Example:
      }
    }
 
+The `Status` and `Reason` can optionally be set in one single call to the 
status setter:
+
+.. code-block:: cpp
+
+       cripts::Error::Status::Set(403, "Go Away");
+
 .. _cripts-misc-transaction:
 
 ATS transactions are generally hidden within Cripts, but for power users, the
diff --git a/include/cripts/Context.hpp b/include/cripts/Context.hpp
index 6e1520e283..e3592aefe2 100644
--- a/include/cripts/Context.hpp
+++ b/include/cripts/Context.hpp
@@ -102,7 +102,7 @@ private:
 
 // This may be weird, but oh well for now.
 #define Get()               _get(context)
-#define Set(_value)         _set(context, _value)
+#define Set(_value, ...)    _set(context, _value __VA_OPT__(, ) __VA_ARGS__)
 #define Update()            _update(context)
 #define RunRemap()          _runRemap(context)
 #define Activate()          _activate(context->p_instance)
diff --git a/include/cripts/Error.hpp b/include/cripts/Error.hpp
index d1aefea9a4..da674abe60 100644
--- a/include/cripts/Error.hpp
+++ b/include/cripts/Error.hpp
@@ -71,12 +71,12 @@ public:
     Status(const self_type &)         = delete;
     void operator=(const self_type &) = delete;
 
-    static void _set(cripts::Context *context, TSHttpStatus status);
+    static void _set(cripts::Context *context, TSHttpStatus status, const 
cripts::string_view msg = {});
 
     static void
-    _set(cripts::Context *context, int status)
+    _set(cripts::Context *context, int status, const cripts::string_view msg = 
{})
     {
-      _set(context, static_cast<TSHttpStatus>(status));
+      _set(context, static_cast<TSHttpStatus>(status), msg);
     }
 
     static TSHttpStatus _get(cripts::Context *context);
diff --git a/src/cripts/Error.cc b/src/cripts/Error.cc
index b1076d6f82..606c05cbbd 100644
--- a/src/cripts/Error.cc
+++ b/src/cripts/Error.cc
@@ -44,12 +44,17 @@ Error::Reason::_set(cripts::Context *context, const 
cripts::string_view msg)
   context->state.error._reason._setter(msg);
 }
 
+// For convenience, an optional Reason message can also be specified with the 
status
 void
-Error::Status::_set(cripts::Context *context, TSHttpStatus status)
+Error::Status::_set(cripts::Context *context, TSHttpStatus status, const 
cripts::string_view msg)
 {
   context->state.error.Fail();
   context->state.error._status._setter(status);
 
+  if (msg.size() > 0) {
+    context->state.error._reason._setter(msg);
+  }
+
   if (context->state.error.Redirected() || status == 
TS_HTTP_STATUS_MOVED_PERMANENTLY ||
       status == TS_HTTP_STATUS_MOVED_TEMPORARILY || status == 
TS_HTTP_STATUS_TEMPORARY_REDIRECT ||
       status == TS_HTTP_STATUS_PERMANENT_REDIRECT) {

Reply via email to