Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Leonid Vasiliev <lvasiliev@tarantool.org>,
	alexander.turenko@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 2/6] error: Add the custom error type
Date: Mon, 6 Apr 2020 00:14:33 +0200	[thread overview]
Message-ID: <c0fcb757-d57e-5321-7b32-bcf59c638ed2@tarantool.org> (raw)
In-Reply-To: <56090ffde2ff472e2add51df13ebac93f5ee66b5.1585053743.git.lvasiliev@tarantool.org>

Thanks for the patch!

See 6 comments below.

On 24/03/2020 13:46, Leonid Vasiliev wrote:
> A possibility to create an error with a custom subtype was added.
> 
> In accordance with https://github.com/tarantool/tarantool/issues/4398
> a custom error type has been added to the box.error.
> Now, it's possible to create an error with a custom subtype (string value)
> for use it in applications.
> 
> @TarantoolBot document
> Title: error.custom_type
> A custom error type has been added to the box.error.
> Now, it's possible to create an error with a custom subtype (string value)
> for use it in applications.
> 
> Example:
> 
> err_custom = box.error.new(box.error.CUSTOM_ERROR, "My Custom Type", "Reason")
> Now:
> err_custom.type == "CustomError"
> err_custom.custom_type == "My Custom Type"

1. Regarding types we discussed that verbally and then I
wrote it in the ticket, that better return custom type in
err.type. And return the original type in a new field. To
simplify cascade error checking, when one type is compared
to many possible types to make appropriate actions. Just for
record.

> err_custom.message == "User custom error: Reason"

2. The whole point of having custom errors - be fully customizable.
I don't think it is a good idea to seal this prefix into every
user defined error. After all, usually errors are created not for
end users, but for systems built on top of tarantool. Hence it
looks odd to even mention 'User'.

3. In the doc request you didn't say a word about custom error
type length limit.

> diff --git a/src/box/error.cc b/src/box/error.cc
> index 47dce33..25e7eff 100644
> --- a/src/box/error.cc
> +++ b/src/box/error.cc
> @@ -253,3 +274,38 @@ BuildAccessDeniedError(const char *file, unsigned int line,
>  		return e;
>  	}
>  }
> +
> +static struct method_info customerror_methods[] = {
> +	make_method(&type_CustomError, "custom_type", &CustomError::custom_type),
> +	METHODS_SENTINEL
> +};
> +
> +const struct type_info type_CustomError =
> +	make_type("CustomError", &type_ClientError,
> +		  customerror_methods);
> +
> +CustomError::CustomError(const char *file, unsigned int line,
> +			 const char *custom_type)
> +	:ClientError(&type_CustomError, file, line, ER_CUSTOM_ERROR)
> +{
> +	error_format_msg(this, tnt_errcode_desc(m_errcode),
> +			 custom_type ?: "");
> +
> +	if (custom_type) {
> +		strncpy(m_custom_type, custom_type, 63);
> +		m_custom_type[63] = '\0';

4. Please, don't do that. Never use constants the same constant
in several places, and depending on other constants. Use
sizeof(m_custom_type) - 1.

> +	} else {
> +		m_custom_type[0] = '\0';
> +	}
> +}
> diff --git a/src/box/error.h b/src/box/error.h
> index b8c7cf7..3e0beb8 100644
> --- a/src/box/error.h
> +++ b/src/box/error.h
> @@ -70,6 +73,14 @@ const char *
>  box_error_type(const box_error_t *error);
>  
>  /**
> + * Return the error custom type,
> + * \param error
> + * \return pointer to custom error type. On error, return NULL
> + */
> +const char *
> +box_custom_error_type(const box_error_t *e);
> +

5. Please, move this out of '\cond public'. I don't think
we are ready to release custom errors as part of the public
C API. The same for box_custom_error_set().

> +/**
>   * Return IPROTO error code
>   * \param error error
>   * \return enum box_error_code
> diff --git a/src/lua/error.lua b/src/lua/error.lua
> index 765ce73..26abec8 100644
> --- a/src/lua/error.lua
> +++ b/src/lua/error.lua
> @@ -148,11 +152,16 @@ local function error_serialize(err)
>      return error_message(err)
>  end
>  
> +local function error_is_custom(err)
> +    return ffi.string(err._type.name) == 'CustomError'
> +end
> +
>  local error_methods = {
> -    ["unpack"] = error_unpack;
> -    ["raise"] = error_raise;
> -    ["match"] = error_match; -- Tarantool 1.6 backward compatibility
> -    ["__serialize"] = error_serialize;
> +    ["unpack"] = error_unpack,
> +    ["raise"] = error_raise,
> +    ["match"] = error_match, -- Tarantool 1.6 backward compatibility
> +    ["is_custom"] = error_is_custom,
> +    ["__serialize"] = error_serialize

6. The same as in the previous patch - try not to change
existing code when possible. Here it is possible. And below too.

>  }
>  
>  local function error_index(err, key)
> @@ -181,11 +190,11 @@ local function error_concat(lhs, rhs)
>  end
>  
>  local error_mt = {
> -    __index = error_index;
> -    __tostring = error_message;
> -    __concat = error_concat;
> +    __index = error_index,
> +    __tostring = error_message,
> +    __concat = error_concat
>  };
>  
> -ffi.metatype('struct error', error_mt);
> +ffi.metatype('struct error', error_mt)
>  
>  return error

  reply	other threads:[~2020-04-05 22:14 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24 12:45 [Tarantool-patches] [PATCH 0/6] Extending error functionality Leonid Vasiliev
2020-03-24 12:45 ` [Tarantool-patches] [PATCH 1/6] error: Add a Lua backtrace to error Leonid Vasiliev
2020-04-05 22:14   ` Vladislav Shpilevoy
2020-04-08 13:56     ` Igor Munkin
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 2/6] error: Add the custom error type Leonid Vasiliev
2020-04-05 22:14   ` Vladislav Shpilevoy [this message]
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 3/6] iproto: Add negotiation phase Leonid Vasiliev
2020-03-24 20:02   ` Konstantin Osipov
2020-03-25  7:35     ` lvasiliev
2020-03-25  8:42       ` Konstantin Osipov
2020-03-25 10:56         ` Eugene Leonovich
2020-03-25 11:13           ` Konstantin Osipov
2020-03-26 11:37           ` lvasiliev
2020-03-26 11:18         ` lvasiliev
2020-03-26 12:16           ` Konstantin Osipov
2020-03-26 12:54             ` Kirill Yukhin
2020-03-26 13:19               ` Konstantin Osipov
2020-03-26 13:31                 ` Konstantin Osipov
2020-03-26 21:13       ` Alexander Turenko
2020-03-26 21:53         ` Alexander Turenko
2020-03-27  8:28         ` Konstantin Osipov
2020-03-26 23:35       ` Alexander Turenko
2020-03-27  8:39         ` Konstantin Osipov
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 4/6] error: Add extended error transfer format Leonid Vasiliev
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 5/6] error: Add test for extended error Leonid Vasiliev
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 6/6] error: Transmit an error through IPROTO_OK as object Leonid Vasiliev
2020-03-27 23:11 ` [Tarantool-patches] [PATCH 0/6] Extending error functionality lvasiliev
2020-03-28 13:54   ` Alexander Turenko
2020-03-30 10:48     ` lvasiliev
2020-04-01 15:35 ` Alexander Turenko

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=c0fcb757-d57e-5321-7b32-bcf59c638ed2@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=lvasiliev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2/6] error: Add the custom error type' \
    /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