2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module builtin
|
2020-12-19 01:27:35 +03:00
|
|
|
|
2021-03-12 21:05:05 +03:00
|
|
|
// IError holds information about an error instance
|
|
|
|
pub interface IError {
|
2022-02-11 16:52:33 +03:00
|
|
|
// >> Hack to allow old style custom error implementations
|
|
|
|
// TODO: remove once deprecation period for `IError` methods has ended
|
2021-03-14 13:17:31 +03:00
|
|
|
msg string
|
2022-02-11 16:52:33 +03:00
|
|
|
code int // <<
|
|
|
|
msg() string
|
|
|
|
code() int
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns the message of IError
|
2022-02-11 16:52:33 +03:00
|
|
|
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
|
2022-02-12 12:54:10 +03:00
|
|
|
// TODO: remove once deprecation period for `IError` methods has ended
|
2022-04-11 21:49:52 +03:00
|
|
|
// old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between
|
|
|
|
// <<
|
2022-11-15 16:53:13 +03:00
|
|
|
'${err.type_name()}: ${err.msg()}'
|
2022-02-11 16:52:33 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
|
2022-02-11 16:52:33 +03:00
|
|
|
// Error is the empty default implementation of `IError`.
|
2022-02-12 12:54:10 +03:00
|
|
|
pub struct Error {}
|
2022-02-11 16:52:33 +03:00
|
|
|
|
|
|
|
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 {
|
2021-03-12 21:05:05 +03:00
|
|
|
pub:
|
|
|
|
msg string
|
|
|
|
code int
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// msg returns the message of MessageError
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err MessageError) msg() string {
|
2022-08-16 18:21:28 +03:00
|
|
|
if err.code > 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${err.msg}; code: ${err.code}'
|
2022-08-16 18:21:28 +03:00
|
|
|
}
|
2022-02-11 16:52:33 +03:00
|
|
|
return err.msg
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// code returns the code of MessageError
|
2022-02-11 16:52:33 +03:00
|
|
|
pub fn (err MessageError) code() int {
|
|
|
|
return err.code
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
pub fn (err &MessageError) free() {
|
|
|
|
unsafe { err.msg.free() }
|
2021-03-20 02:55:16 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 20:13:50 +03:00
|
|
|
const none__ = IError(&None__{})
|
|
|
|
|
|
|
|
struct None__ {
|
2022-02-11 16:52:33 +03:00
|
|
|
Error
|
2021-03-12 21:05:05 +03:00
|
|
|
}
|
|
|
|
|
2021-03-14 13:17:31 +03:00
|
|
|
fn (_ None__) str() string {
|
|
|
|
return 'none'
|
|
|
|
}
|
2021-03-13 20:13:50 +03:00
|
|
|
|
2021-06-22 10:30:14 +03:00
|
|
|
[if trace_error ?]
|
2021-05-18 20:02:56 +03:00
|
|
|
fn trace_error(x string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('> ${@FN} | ${x}')
|
2021-05-18 20:02:56 +03:00
|
|
|
}
|
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// error returns a default error instance containing the error given in `message`.
|
2022-03-31 19:32:32 +03:00
|
|
|
// Example: if ouch { return error('an error occurred') }
|
2021-03-14 03:54:46 +03:00
|
|
|
[inline]
|
|
|
|
pub fn error(message string) IError {
|
2021-05-18 20:02:56 +03:00
|
|
|
trace_error(message)
|
2022-02-11 16:52:33 +03:00
|
|
|
return &MessageError{
|
2021-03-14 03:54:46 +03:00
|
|
|
msg: message
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 21:05:05 +03:00
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
2022-03-31 19:32:32 +03:00
|
|
|
// Example: if ouch { return error_with_code('an error occurred', 1) }
|
2021-03-14 03:54:46 +03:00
|
|
|
[inline]
|
|
|
|
pub fn error_with_code(message string, code int) IError {
|
2022-11-15 16:53:13 +03:00
|
|
|
trace_error('${message} | code: ${code}')
|
2022-02-11 16:52:33 +03:00
|
|
|
return &MessageError{
|
2021-03-14 03:54:46 +03:00
|
|
|
msg: message
|
|
|
|
code: code
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 19:44:15 +03:00
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// Option is the base of V's internal optional return system.
|
|
|
|
struct Option {
|
2022-04-15 14:45:52 +03:00
|
|
|
state u8
|
2021-03-14 03:54:46 +03:00
|
|
|
err IError = none__
|
2021-02-22 19:44:15 +03:00
|
|
|
// Data is trailing after err
|
|
|
|
// and is not included in here but in the
|
2021-03-14 03:54:46 +03:00
|
|
|
// derived Option_xxx types
|
2021-02-22 19:44:15 +03:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:25:54 +03:00
|
|
|
// option is the base of V's internal optional return system.
|
|
|
|
struct _option {
|
|
|
|
state u8
|
|
|
|
err IError = none__
|
|
|
|
// Data is trailing after err
|
|
|
|
// and is not included in here but in the
|
|
|
|
// derived _option_xxx types
|
|
|
|
}
|
|
|
|
|
2022-05-22 14:52:38 +03:00
|
|
|
fn _option_ok(data voidptr, mut option _option, size int) {
|
|
|
|
unsafe {
|
|
|
|
*option = _option{}
|
|
|
|
// use err to get the end of OptionBase and then memcpy into it
|
|
|
|
vmemcpy(&u8(&option.err) + sizeof(IError), data, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:10:30 +03:00
|
|
|
struct _result {
|
2022-04-30 01:59:14 +03:00
|
|
|
is_error bool
|
|
|
|
err IError = none__
|
|
|
|
// Data is trailing after err
|
|
|
|
// and is not included in here but in the
|
|
|
|
// derived Result_xxx types
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:10:30 +03:00
|
|
|
fn _result_ok(data voidptr, mut res _result, size int) {
|
2022-04-30 01:59:14 +03:00
|
|
|
unsafe {
|
2022-04-30 17:10:30 +03:00
|
|
|
*res = _result{}
|
2022-04-30 01:59:14 +03:00
|
|
|
// use err to get the end of ResultBase and then memcpy into it
|
|
|
|
vmemcpy(&u8(&res.err) + sizeof(IError), data, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 12:41:51 +03:00
|
|
|
pub fn (_ none) str() string {
|
|
|
|
return 'none'
|
|
|
|
}
|