1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: change IError msg and code to methods + fix vlib, add a deprecation notice for the old usages (#13041)

This commit is contained in:
Tim Basel
2022-02-11 14:52:33 +01:00
committed by GitHub
parent 61024d4b75
commit 9d0a5942ac
80 changed files with 493 additions and 324 deletions

View File

@ -14,41 +14,85 @@ pub fn panic(s string) {
// IError holds information about an error instance
pub interface IError {
// >> Hack to allow old style custom error implementations
// TODO: remove once deprecation period for `IError` methods has ended
msg string
code int
code int // <<
msg() string
code() int
}
// Error is the default implementation of IError, that is returned by e.g. `error()`
pub fn (err IError) str() string {
return match err {
None__ {
'none'
}
Error {
err.msg()
}
MessageError {
err.msg()
}
else {
// >> Hack to allow old style custom error implementations
// TODO: can be removed once the checker 'hacks' are merged (so `vc` has them included)
if !isnil(err.msg) {
'$err.type_name(): $err.msg'
} else {
// <<
'$err.type_name(): $err.msg()'
}
}
}
}
// Error is the empty default implementation of `IError`.
pub struct Error {
// >> Hack to allow old style custom error implementations
// TODO: can be removed once the checker 'hacks' are merged (so `vc` has them included)
msg string
code int
/// <<
}
pub fn (err Error) msg() string {
return ''
}
pub fn (err Error) code() int {
return 0
}
// MessageError is the default implementation of the `IError` interface that is returned by the `error()` function
struct MessageError {
pub:
msg string
code int
}
pub fn (err MessageError) msg() string {
return err.msg
}
pub fn (err MessageError) code() int {
return err.code
}
pub const none__ = IError(&None__{})
struct None__ {
msg string
code int
Error
}
fn (_ None__) str() string {
return 'none'
}
pub const none__ = IError(None__{'', 0})
pub struct Option {
state byte
err IError = none__
}
pub fn (err IError) str() string {
return match err {
None__ { 'none' }
Error { err.msg }
else { 'Error: $err.msg' }
}
}
pub fn (o Option) str() string {
if o.state == 0 {
return 'Option{ ok }'
@ -69,7 +113,7 @@ fn trace_error(x string) {
[inline]
pub fn error(message string) IError {
// trace_error(message)
return &Error{
return &MessageError{
msg: message
}
}
@ -79,7 +123,7 @@ pub fn error(message string) IError {
[inline]
pub fn error_with_code(message string, code int) IError {
// trace_error('$message | code: $code')
return &Error{
return &MessageError{
msg: message
code: code
}