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