Skip to content

Commit

Permalink
builtin: simplify MessageError.msg() (#21524)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed May 18, 2024
1 parent c8bf38c commit 3a3a108
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 15 deletions.
7 changes: 6 additions & 1 deletion vlib/builtin/js/builtin.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn (err IError) str() string {
err.msg()
}
MessageError {
err.msg()
err.str()
}
else {
// >> Hack to allow old style custom error implementations
Expand Down Expand Up @@ -68,6 +68,11 @@ pub:
code int
}

// str returns the message and code of the MessageError
pub fn (err MessageError) str() string {
return err.msg
}

// msg returns the message of the MessageError
pub fn (err MessageError) msg() string {
return err.msg
Expand Down
13 changes: 9 additions & 4 deletions vlib/builtin/result.v
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn (err IError) str() string {
err.msg()
}
MessageError {
err.msg()
(*err).str()
}
else {
// >> Hack to allow old style custom error implementations
Expand Down Expand Up @@ -70,15 +70,20 @@ pub:
code int
}

// msg returns the message of MessageError
pub fn (err MessageError) msg() string {
// str returns both the .msg and .code of MessageError, when .code is != 0
pub fn (err MessageError) str() string {
if err.code > 0 {
return '${err.msg}; code: ${err.code}'
}
return err.msg
}

// code returns the code of MessageError
// msg returns only the message of MessageError
pub fn (err MessageError) msg() string {
return err.msg
}

// code returns only the code of MessageError
pub fn (err MessageError) code() int {
return err.code
}
Expand Down
3 changes: 2 additions & 1 deletion vlib/os/signal_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fn test_signal_opt_invalid_argument() {
assert false
}
os.signal_opt(.kill, default_handler) or {
assert err.msg() == 'Invalid argument; code: 22'
assert err.str() == 'Invalid argument; code: 22'
assert err.msg() == 'Invalid argument'
assert err.code() == 22
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
}
ast.PostfixExpr {
ifdef := g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true) or {
verror(err.msg())
verror(err.str())
return false, true
}
g.write('defined(${ifdef})')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
================ V panic ================
module: main
function: main()
message: no such table: User (1) (INSERT INTO `User` (`id`, `name`) VALUES (?1, ?2);); code: 1
message: no such table: User (1) (INSERT INTO `User` (`id`, `name`) VALUES (?1, ?2);)
file: vlib/v/slow_tests/inout/orm_panic_for_insert_into_not_created_table.vv:17
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
================ V panic ================
module: main
function: main()
message: no such table: User (1) (SELECT `id`, `name` FROM `User`;); code: 1
message: no such table: User (1) (SELECT `id`, `name` FROM `User`;)
file: vlib/v/slow_tests/inout/orm_panic_for_select_from_not_created_table.vv:13
8 changes: 5 additions & 3 deletions vlib/v/tests/option_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ fn test_err_with_code() {
assert false
_ := w
} else {
assert err.msg() == 'hi; code: 137'
assert err.str() == 'hi; code: 137'
assert err.msg() == 'hi'
assert err.code() == 137
}
v := opt_err_with_code(56) or {
assert err.msg() == 'hi; code: 56'
assert err.str() == 'hi; code: 56'
assert err.msg() == 'hi'
assert err.code() == 56
return
}
Expand Down Expand Up @@ -297,7 +299,7 @@ fn test_option_void_return_types_of_anon_fn() {
}

struct Foo {
f fn (int) !
f fn (int) ! = unsafe { nil }
}

fn test_option_void_return_types_of_anon_fn_in_struct() {
Expand Down
6 changes: 4 additions & 2 deletions vlib/v/tests/results_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ fn test_err_with_code() {
assert false
_ := w
} else {
assert err.msg() == 'hi; code: 137'
assert err.str() == 'hi; code: 137'
assert err.msg() == 'hi'
assert err.code() == 137
}
v := res_err_with_code(56) or {
assert err.msg() == 'hi; code: 56'
assert err.str() == 'hi; code: 56'
assert err.msg() == 'hi'
assert err.code() == 56
return
}
Expand Down
1 change: 0 additions & 1 deletion vlib/vweb/tests/controller_duplicate_server.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn main() {
}
http_port := os.args[1].int()
assert http_port > 0
timeout := os.args[2].int()
mut app_dup := &App{
controllers: [
vweb.controller('/admin', &Admin{}),
Expand Down

0 comments on commit 3a3a108

Please sign in to comment.