Commit Graph

18 Commits

Author SHA1 Message Date
Sam Atkins
1a4dd47d5f AK: VERIFY inside release_value_but_fixme_should_propagate_errors()
While the code did already VERIFY that the ErrorOr holds a value, this
was done by Variant, so the error message was just that `has<T>()` is
false. This is less helpful than I would like, especially if backtraces
are not working and this is all you have to go on. Adding this extra
VERIFY means the assertion message (`!is_error()`) is easier to
understand.
2022-02-16 19:49:41 -05:00
Andreas Kling
452a5531be AK+LibMain: Improve formatter for AK::Error in userspace
Print the full associated string metadata by default (if available.)
2021-12-20 21:13:42 +01:00
sin-ack
69ef211925 Kernel+LibC: Move errno definitions to Kernel/API/POSIX
This fixes at least half of our LibC includes in the kernel. The source
of truth for errno codes and their description strings now lives in
Kernel/API/POSIX/errno.h as an enumeration, which LibC includes.
2021-12-16 22:21:35 +03:30
Ben Wiederhake
208d85e707 AK+Tests: Use less space in ErrorOr 2021-12-16 09:32:51 +01:00
Ben Wiederhake
6f37510a71 AK: Implement missing const getters in AK::Error, fix typo
Note that the return type for the non-const method error() changed. This
is most likely an accident, hidden by the fact that ErrorType typically
is Error.
2021-12-05 22:59:09 +01:00
Andreas Kling
4e530135d5 AK+LibSystem+LibMain: Add Error::from_syscall() for syscall failures
This creates an error that contains the name of the syscall that failed.
This allows error handlers to print out the name of the call if they
want to. :^)
2021-11-22 19:28:31 +01:00
Andreas Kling
216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Andreas Kling
11aad74dce AK: Forward declare Error and ErrorOr in AK/Forward.h 2021-11-17 00:21:12 +01:00
Andreas Kling
7ee10c6926 AK: Add some more ways to construct Error and ErrorOr<T>
This is preparation for using Error in the kernel instead of KResult.
2021-11-08 00:36:35 +01:00
Andreas Kling
5e473a63d3 AK: Make Error.h pull in Try.h
Users of this API will want TRY().
2021-11-08 00:36:19 +01:00
Andreas Kling
e253cf694e AK: Allow subclassing Error 2021-11-08 00:35:27 +01:00
Andreas Kling
202950bb01 AK: Make Error and ErrorOr<T> work in Lagom as well :^)
ErrnoCode is not a thing outside __serenity__, so let's not make
assumptions about it existing.
2021-11-08 00:35:27 +01:00
Andreas Kling
2116620db8 AK: Add ErrorOr<T>::release_value_but_fixme_should_propagate_errors()
This is an alternative to ErrorOr<T>::release_value() that can be used
when converting code to signal that we're releasing the value without
error propagation as a way to move forward now.

This makes these cases much easier to find later on, once more paths for
error propagation are available.
2021-11-08 00:35:27 +01:00
Andreas Kling
e5dde37e24 AK: Bring AK::Error into the global namespace 2021-11-08 00:35:27 +01:00
Andreas Kling
c4edb9f6c2 AK: Add Error and ErrorOr<T>
The goal with these is to eventually replace AK::Result, KResult and
KResultOr<T> with something that works (and makes sense) in both kernel
and userspace.

This first cut of Error can be made from an errno code, or from a string
literal (StringView)
2021-11-08 00:35:27 +01:00
Andreas Kling
59cd181ed9 AK: Remove unused Error template 2020-04-21 16:19:18 +02:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Robin Burchell
7dd25141cd Add Error<>
Put simply, Error<> is a way of forcing error handling onto an API user.

Given a function like:

    bool might_work();

The following code might have been written previously:

    might_work(); // but what if it didn't?

The easy way to work around this is of course to [[nodiscard]] might_work.
But this doesn't work for more complex cases like, for instance, a
hypothetical read() function which might return one of _many_ errors
(typically signalled with an int, let's say).

    int might_read();

In such a case, the result is often _read_, but not properly handled. Like:

    return buffer.substr(0, might_read()); // but what if might_read returned an error?

This is where Error<> comes in:

    typedef Error<int, 0> ReadError;
    ReadError might_read();

    auto res = might_read();
    if (might_read.failed()) {
        switch (res.value()) {
        case EBADF:
            ...
        }
    }

Error<> uses clang's consumable attributes to force failed() to be
checked on an Error instance. If it's not checked, then you get smacked.
2019-07-31 09:06:39 +02:00