Commit Graph

11 Commits

Author SHA1 Message Date
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