From 18cccb6caf27627c7741afe3864a28ccb61b0d7e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 16 Aug 2022 18:21:28 +0300 Subject: [PATCH] builtin: show non zero codes on bubbled `error_with_code(msg,code)` errors --- vlib/builtin/option.v | 3 +++ vlib/os/signal_test.v | 2 +- vlib/v/tests/option_test.v | 15 ++++++++------- vlib/v/tests/results_test.v | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 02e8235a8..05f9bc44e 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -55,6 +55,9 @@ pub: // msg returns the message of MessageError pub fn (err MessageError) msg() string { + if err.code > 0 { + return '$err.msg; code: $err.code' + } return err.msg } diff --git a/vlib/os/signal_test.v b/vlib/os/signal_test.v index 5e6158fc8..66aa8a4d0 100644 --- a/vlib/os/signal_test.v +++ b/vlib/os/signal_test.v @@ -20,7 +20,7 @@ fn test_signal_opt_invalid_argument() { assert false } os.signal_opt(.kill, default_handler) or { - assert err.msg() == 'Invalid argument' + assert err.msg() == 'Invalid argument; code: 22' assert err.code() == 22 } } diff --git a/vlib/v/tests/option_test.v b/vlib/v/tests/option_test.v index 4b715e645..67a2e8d43 100644 --- a/vlib/v/tests/option_test.v +++ b/vlib/v/tests/option_test.v @@ -1,18 +1,19 @@ -fn opt_err_with_code() ?string { - return error_with_code('hi', 137) +// TODO: remove this after the deprecation period for `?Type` representing both Result and Option passes. +fn opt_err_with_code(code int) ?string { + return error_with_code('hi', code) } fn test_err_with_code() { - if w := opt_err_with_code() { + if w := opt_err_with_code(137) { assert false _ := w } else { - assert err.msg() == 'hi' + assert err.msg() == 'hi; code: 137' assert err.code() == 137 } - v := opt_err_with_code() or { - assert err.msg() == 'hi' - assert err.code() == 137 + v := opt_err_with_code(56) or { + assert err.msg() == 'hi; code: 56' + assert err.code() == 56 return } assert false diff --git a/vlib/v/tests/results_test.v b/vlib/v/tests/results_test.v index 3cca61ea5..084d8bf1b 100644 --- a/vlib/v/tests/results_test.v +++ b/vlib/v/tests/results_test.v @@ -94,3 +94,24 @@ fn test_results_if_guard() { } assert false } + +fn res_err_with_code(code int) !string { + return error_with_code('hi', code) +} + +fn test_err_with_code() { + if w := res_err_with_code(137) { + assert false + _ := w + } else { + assert err.msg() == 'hi; code: 137' + assert err.code() == 137 + } + v := res_err_with_code(56) or { + assert err.msg() == 'hi; code: 56' + assert err.code() == 56 + return + } + assert false + _ := v +} -- 2.30.2