Return generic result for get operations that includes error messages

This commit is contained in:
Tim Wojtulewicz 2025-01-22 17:00:38 -07:00
parent 4695060d75
commit f1a7376e0a
27 changed files with 83 additions and 53 deletions

View file

@ -95,9 +95,11 @@ export {
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
## be returned. The caller should check the validity of the value before
## attempting to use it. If the value is unset, an error string may be
## available to describe the failure.
global get: function(backend: opaque of Storage::BackendHandle, key: any,
async_mode: bool &default=T): any;
async_mode: bool &default=T): val_result;
## Erases an entry from the backend.
##
@ -134,7 +136,7 @@ function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs):
return Storage::__put(backend, args$key, args$value, args$overwrite, args$expire_time, args$async_mode);
}
function get(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T): any
function get(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T): val_result
{
return Storage::__get(backend, key, async_mode);
}

View file

@ -356,6 +356,14 @@ type ftp_port: record {
valid: bool; ##< True if format was right. Only then are *h* and *p* valid.
};
## A generic type for returning either a value or an error string from a
## function or a BIF method. This is sort of equivalent to std::expected
## in C++.
type val_result: record {
val: any &optional;
error: string &optional;
};
## Statistics about what a TCP endpoint sent.
##
## .. zeek:see:: conn_stats