Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org,
	vdavydov@tarantool.org
Subject: [Tarantool-patches] [PATCH 4/9] error: use error_payload to store optional members
Date: Sat,  6 Nov 2021 00:56:35 +0100	[thread overview]
Message-ID: <c759e56099240a2791a6da42f7dc57db59f8741b.1636156453.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1636156453.git.v.shpilevoy@tarantool.org>

error_payload is a part of struct error now. All the fields stored
in C++ classes on top of struct error are moved into the payload.

Part of #5568
---
 src/box/error.cc        | 11 +++----
 src/box/error.h         | 34 +++++---------------
 src/lib/core/diag.c     |  2 ++
 src/lib/core/diag.h     | 70 +++++++++++++++++++++++++++++++++++++++++
 src/lib/uuid/mp_uuid.c  | 16 +++++++++-
 src/lib/uuid/tt_uuid.h  |  9 ++++++
 src/lua/error.lua       | 12 +++++++
 test/box/error.result   |  4 +--
 test/box/error.test.lua |  2 +-
 9 files changed, 124 insertions(+), 36 deletions(-)

diff --git a/src/box/error.cc b/src/box/error.cc
index bc0d46601..629ec703f 100644
--- a/src/box/error.cc
+++ b/src/box/error.cc
@@ -305,9 +305,9 @@ AccessDeniedError::AccessDeniedError(const char *file, unsigned int line,
 	 */
 	if (run_trigers)
 		trigger_run(&on_access_denied, (void *) &ctx);
-	m_object_type = strdup(object_type);
-	m_access_type = strdup(access_type);
-	m_object_name = strdup(object_name);
+	error_field_set_str(this, "object_type", object_type);
+	error_field_set_str(this, "object_name", object_name);
+	error_field_set_str(this, "access_type", access_type);
 }
 
 struct error *
@@ -337,15 +337,14 @@ CustomError::CustomError(const char *file, unsigned int line,
 			 const char *custom_type, uint32_t errcode)
 	:ClientError(&type_CustomError, file, line, errcode)
 {
-	strncpy(m_custom_type, custom_type, sizeof(m_custom_type) - 1);
-	m_custom_type[sizeof(m_custom_type) - 1] = '\0';
+	error_field_set_str(this, "custom_type", custom_type);
 }
 
 void
 CustomError::log() const
 {
 	say_file_line(S_ERROR, file, line, errmsg,
-		      "Custom type %s", m_custom_type);
+		      "Custom type %s", custom_type());
 }
 
 struct error *
diff --git a/src/box/error.h b/src/box/error.h
index 30ab36a97..43faaea70 100644
--- a/src/box/error.h
+++ b/src/box/error.h
@@ -243,38 +243,23 @@ public:
 			  const char *object_name, const char *user_name,
 			  bool run_trigers = true);
 
-	~AccessDeniedError()
-	{
-		free(m_object_name);
-		free(m_object_type);
-		free(m_access_type);
-	}
-
 	const char *
-	object_type()
+	object_type() const
 	{
-		return m_object_type;
+		return error_field_get_str(this, "object_type");
 	}
 
 	const char *
-	object_name()
+	object_name() const
 	{
-		return m_object_name?:"(nil)";
+		return error_field_get_str(this, "object_name");
 	}
 
 	const char *
-	access_type()
+	access_type() const
 	{
-		return m_access_type;
+		return error_field_get_str(this, "access_type");
 	}
-
-private:
-	/** Type of object the required access was denied to */
-	char *m_object_type;
-	/** Name of object the required access was denied to */
-	char *m_object_name;
-	/** Type of declined access */
-	char *m_access_type;
 };
 
 /**
@@ -319,13 +304,10 @@ public:
 	virtual void log() const;
 
 	const char*
-	custom_type()
+	custom_type() const
 	{
-		return m_custom_type;
+		return error_field_get_str(this, "custom_type");
 	}
-private:
-	/** Custom type name. */
-	char m_custom_type[64];
 };
 
 #endif /* defined(__cplusplus) */
diff --git a/src/lib/core/diag.c b/src/lib/core/diag.c
index b557ca667..217c3ae7e 100644
--- a/src/lib/core/diag.c
+++ b/src/lib/core/diag.c
@@ -53,6 +53,7 @@ error_unref(struct error *e)
 			to_delete->cause->effect = NULL;
 			to_delete->cause = NULL;
 		}
+		error_payload_destroy(&to_delete->payload);
 		to_delete->destroy(to_delete);
 		if (cause == NULL)
 			return;
@@ -117,6 +118,7 @@ error_create(struct error *e,
 	e->refs = 0;
 	e->saved_errno = 0;
 	e->code = 0;
+	error_payload_create(&e->payload);
 	if (file != NULL) {
 		snprintf(e->file, sizeof(e->file), "%s", file);
 		e->line = line;
diff --git a/src/lib/core/diag.h b/src/lib/core/diag.h
index 30b8d2f5d..03bd223e1 100644
--- a/src/lib/core/diag.h
+++ b/src/lib/core/diag.h
@@ -36,6 +36,8 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <assert.h>
+
+#include "error_payload.h"
 #include "say.h"
 
 #if defined(__cplusplus)
@@ -89,6 +91,8 @@ struct error {
 	int saved_errno;
 	/** Error code. Shortest possible description of error's reason. */
 	int code;
+	/** Key-value storage with error's dynamic fields. */
+	struct error_payload payload;
 	/** Line number. */
 	unsigned line;
 	/* Source file name. */
@@ -120,6 +124,72 @@ error_ref(struct error *e);
 void
 error_unref(struct error *e);
 
+static inline const char *
+error_field_get_str(const struct error *e, const char *name)
+{
+	return error_payload_get_str(&e->payload, name);
+}
+
+static inline void
+error_field_set_str(struct error *e, const char *name, const char *value)
+{
+	error_payload_set_str(&e->payload, name, value);
+}
+
+static inline bool
+error_field_get_uint(const struct error *e, const char *name, uint64_t *value)
+{
+	return error_payload_get_uint(&e->payload, name, value);
+}
+
+static inline void
+error_field_set_uint(struct error *e, const char *name, uint64_t value)
+{
+	error_payload_set_uint(&e->payload, name, value);
+}
+
+static inline bool
+error_field_get_int(const struct error *e, const char *name, int64_t *value)
+{
+	return error_payload_get_int(&e->payload, name, value);
+}
+
+static inline void
+error_field_set_int(struct error *e, const char *name, int64_t value)
+{
+	error_payload_set_int(&e->payload, name, value);
+}
+
+static inline bool
+error_field_get_double(const struct error *e, const char *name, double *value)
+{
+	return error_payload_get_double(&e->payload, name, value);
+}
+
+static inline void
+error_field_set_double(struct error *e, const char *name, double value)
+{
+	error_payload_set_double(&e->payload, name, value);
+}
+
+static inline bool
+error_field_get_bool(const struct error *e, const char *name, bool *value)
+{
+	return error_payload_get_bool(&e->payload, name, value);
+}
+
+static inline void
+error_field_set_bool(struct error *e, const char *name, bool value)
+{
+	error_payload_set_bool(&e->payload, name, value);
+}
+
+static inline void
+error_field_unset(struct error *e, const char *name)
+{
+	error_payload_unset(&e->payload, name);
+}
+
 /**
  * Unlink error from its effect. For instance:
  * e1 -> e2 -> e3 -> e4 (e1:set_prev(e2); e2:set_prev(e3) ...)
diff --git a/src/lib/uuid/mp_uuid.c b/src/lib/uuid/mp_uuid.c
index a60716eb5..51f0465ac 100644
--- a/src/lib/uuid/mp_uuid.c
+++ b/src/lib/uuid/mp_uuid.c
@@ -32,7 +32,7 @@
 #include "mp_uuid.h"
 #include "msgpuck.h"
 #include "mp_extension_types.h"
-#include "error_payload.h"
+#include "diag.h"
 
 inline uint32_t
 mp_sizeof_uuid(void)
@@ -139,3 +139,17 @@ error_payload_set_uuid(struct error_payload *e, const char *name,
 	char *data = error_payload_prepare(e, name, mp_sizeof_uuid(), 0)->data;
 	mp_encode_uuid(data, uuid);
 }
+
+bool
+error_field_get_uuid(const struct error *e, const char *name,
+		     struct tt_uuid *uuid)
+{
+	return error_payload_get_uuid(&e->payload, name, uuid);
+}
+
+void
+error_field_set_uuid(struct error *e, const char *name,
+		     const struct tt_uuid *uuid)
+{
+	error_payload_set_uuid(&e->payload, name, uuid);
+}
diff --git a/src/lib/uuid/tt_uuid.h b/src/lib/uuid/tt_uuid.h
index 866764e77..eb457c1f1 100644
--- a/src/lib/uuid/tt_uuid.h
+++ b/src/lib/uuid/tt_uuid.h
@@ -41,6 +41,7 @@
 extern "C" {
 #endif
 
+struct error;
 struct error_payload;
 
 enum { UUID_LEN = 16, UUID_STR_LEN = 36 };
@@ -206,6 +207,14 @@ void
 error_payload_set_uuid(struct error_payload *e, const char *name,
 		       const struct tt_uuid *uuid);
 
+bool
+error_field_get_uuid(const struct error *e, const char *name,
+		     struct tt_uuid *uuid);
+
+void
+error_field_set_uuid(struct error *e, const char *name,
+		     const struct tt_uuid *uuid);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif
diff --git a/src/lua/error.lua b/src/lua/error.lua
index 9fb227df4..a5d1d2a43 100644
--- a/src/lua/error.lua
+++ b/src/lua/error.lua
@@ -11,6 +11,17 @@ enum {
 
 typedef void (*error_f)(struct error *e);
 
+struct error_field {
+    uint32_t _size;
+    const char *_data;
+    char _name[0];
+};
+
+struct error_payload {
+    int _count;
+    struct error_field **_fields;
+};
+
 struct error {
     error_f _destroy;
     error_f _raise;
@@ -19,6 +30,7 @@ struct error {
     int64_t _refs;
     int _saved_errno;
     int code;
+    struct error_payload _payload;
     /** Line number. */
     unsigned _line;
     /* Source file name. */
diff --git a/test/box/error.result b/test/box/error.result
index 19ea53873..7583306a6 100644
--- a/test/box/error.result
+++ b/test/box/error.result
@@ -899,13 +899,13 @@ e:unpack()
  |   - file: '[string "e = box.error.new({type = ''TestType''}) "]'
  |     line: 1
  | ...
--- Try too long type name.
+-- Try long type name.
 e = box.error.new({type = string.rep('a', 128)})
  | ---
  | ...
 #e.type
  | ---
- | - 63
+ | - 128
  | ...
 
 -- gh-4887: accessing 'prev' member also refs it so that after
diff --git a/test/box/error.test.lua b/test/box/error.test.lua
index ed95d1d41..068212d96 100644
--- a/test/box/error.test.lua
+++ b/test/box/error.test.lua
@@ -241,7 +241,7 @@ e:unpack()
 -- Try to omit message.
 e = box.error.new({type = 'TestType'})
 e:unpack()
--- Try too long type name.
+-- Try long type name.
 e = box.error.new({type = string.rep('a', 128)})
 #e.type
 
-- 
2.24.3 (Apple Git-128)


  parent reply	other threads:[~2021-11-05 23:58 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-05 23:56 [Tarantool-patches] [PATCH 0/9] ER_READONLY reason Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 1/9] diag: return created error from diag_set() Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 2/9] error: introduce error_payload Vladislav Shpilevoy via Tarantool-patches
2021-11-08 15:14   ` Serge Petrenko via Tarantool-patches
2021-11-11 23:50     ` Vladislav Shpilevoy via Tarantool-patches
2021-11-12  6:29       ` Serge Petrenko via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 3/9] error: move code to struct error from ClientError Vladislav Shpilevoy via Tarantool-patches
2021-11-08 15:15   ` Serge Petrenko via Tarantool-patches
2021-11-11 23:50     ` Vladislav Shpilevoy via Tarantool-patches
2021-11-12  6:31       ` Serge Petrenko via Tarantool-patches
2021-11-05 23:56 ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 5/9] error: use error_payload in MessagePack codecs Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 6/9] error: use error_payload in Lua Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 7/9] luatest: copy config in cluster:build_server() Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 8/9] luatest: add new helpers for 'server' object Vladislav Shpilevoy via Tarantool-patches
2021-11-08 15:16   ` Serge Petrenko via Tarantool-patches
2021-11-11 23:51     ` Vladislav Shpilevoy via Tarantool-patches
2021-11-05 23:56 ` [Tarantool-patches] [PATCH 9/9] box: enrich ER_READONLY with new details Vladislav Shpilevoy via Tarantool-patches
2021-11-06 19:30   ` Cyrill Gorcunov via Tarantool-patches
2021-11-07 16:45     ` Vladislav Shpilevoy via Tarantool-patches
2021-11-07 20:19       ` Cyrill Gorcunov via Tarantool-patches
2021-11-08 15:18   ` Serge Petrenko via Tarantool-patches
2021-11-11 23:52     ` Vladislav Shpilevoy via Tarantool-patches
2021-11-08 14:25 ` [Tarantool-patches] [PATCH 0/9] ER_READONLY reason Vladimir Davydov via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c759e56099240a2791a6da42f7dc57db59f8741b.1636156453.git.v.shpilevoy@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 4/9] error: use error_payload to store optional members' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox