diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 02e8235a8a..05f9bc44e6 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 5e6158fc85..66aa8a4d09 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 4b715e6452..67a2e8d433 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 3cca61ea5e..084d8bf1b3 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 +}