summaryrefslogtreecommitdiff
path: root/error.c
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2022-02-01 17:59:31 +0900
committerYusuke Endoh <mame@ruby-lang.org>2022-02-22 11:55:40 +0900
commit35ff545bb689f5af93ac603ea1f512705e0dc249 (patch)
tree35807db18378f7f2111ba9f983d3a071895d8d1e /error.c
parent3af316fa8c48e33c03159e3b0b3bef329e41dee8 (diff)
downloadruby-35ff545bb689f5af93ac603ea1f512705e0dc249.tar.gz
Exception#detailed_message is added
Also, the default error printer and Exception#full_message use the method instead of `Exception#message` to get the message string. `Exception#detailed_message` calls `Exception#message`, decorates and returns the result. It adds some escape sequences to highlight, and the class name of the exception to the end of the first line of the message. [Feature #18370]
Diffstat (limited to 'error.c')
-rw-r--r--error.c57
1 files changed, 53 insertions, 4 deletions
diff --git a/error.c b/error.c
index 8f9e21dbb5..be4c37cbac 100644
--- a/error.c
+++ b/error.c
@@ -1122,7 +1122,7 @@ static VALUE rb_eNOERROR;
ID ruby_static_id_cause;
#define id_cause ruby_static_id_cause
-static ID id_message, id_backtrace;
+static ID id_message, id_detailed_message, id_backtrace;
static ID id_key, id_matchee, id_args, id_Errno, id_errno, id_i_path;
static ID id_receiver, id_recv, id_iseq, id_local_variables;
static ID id_private_call_p, id_top, id_bottom;
@@ -1224,7 +1224,7 @@ exc_to_s(VALUE exc)
}
/* FIXME: Include eval_error.c */
-void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse);
+void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse);
VALUE
rb_get_message(VALUE exc)
@@ -1235,6 +1235,21 @@ rb_get_message(VALUE exc)
return e;
}
+VALUE
+rb_get_detailed_message(VALUE exc, VALUE opt)
+{
+ VALUE e;
+ if (NIL_P(opt)) {
+ e = rb_check_funcall(exc, id_detailed_message, 0, 0);
+ }
+ else {
+ e = rb_check_funcall_kw(exc, id_detailed_message, 1, &opt, 1);
+ }
+ if (e == Qundef) return Qnil;
+ if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
+ return e;
+}
+
/*
* call-seq:
* Exception.to_tty? -> true or false
@@ -1332,9 +1347,9 @@ exc_full_message(int argc, VALUE *argv, VALUE exc)
str = rb_str_new2("");
errat = rb_get_backtrace(exc);
- emesg = rb_get_message(exc);
+ emesg = rb_get_detailed_message(exc, opt);
- rb_error_write(exc, emesg, errat, str, highlight, order);
+ rb_error_write(exc, emesg, errat, str, opt, highlight, order);
return str;
}
@@ -1354,6 +1369,38 @@ exc_message(VALUE exc)
/*
* call-seq:
+ * exception.detailed_message(highlight: bool, **opt) -> string
+ *
+ * Processes a string returned by #message.
+ *
+ * It may add the class name of the exception to the end of the first line.
+ * Also, when +highlight+ keyword is true, it adds ANSI escape sequences to
+ * make the message bold.
+ *
+ * If you override this method, it must be tolerant for unknown keyword
+ * arguments. All keyword arguments passed to #full_message are delegated
+ * to this method.
+ *
+ * This method is overridden by did_you_mean and error_highlight to add
+ * their information.
+ */
+
+static VALUE
+exc_detailed_message(int argc, VALUE *argv, VALUE exc)
+{
+ VALUE opt;
+
+ rb_scan_args(argc, argv, "0:", &opt);
+
+ VALUE highlight = check_highlight_keyword(opt);
+
+ extern VALUE rb_decorate_message(const VALUE eclass, const VALUE emesg, int highlight);
+
+ return rb_decorate_message(CLASS_OF(exc), rb_get_message(exc), highlight);
+}
+
+/*
+ * call-seq:
* exception.inspect -> string
*
* Return this exception's class name and message.
@@ -2908,6 +2955,7 @@ Init_Exception(void)
rb_define_method(rb_eException, "==", exc_equal, 1);
rb_define_method(rb_eException, "to_s", exc_to_s, 0);
rb_define_method(rb_eException, "message", exc_message, 0);
+ rb_define_method(rb_eException, "detailed_message", exc_detailed_message, -1);
rb_define_method(rb_eException, "full_message", exc_full_message, -1);
rb_define_method(rb_eException, "inspect", exc_inspect, 0);
rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
@@ -2995,6 +3043,7 @@ Init_Exception(void)
id_cause = rb_intern_const("cause");
id_message = rb_intern_const("message");
+ id_detailed_message = rb_intern_const("detailed_message");
id_backtrace = rb_intern_const("backtrace");
id_key = rb_intern_const("key");
id_matchee = rb_intern_const("matchee");