1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/Error.h>
#include <errno.h>
namespace Audio {
struct LoaderError {
enum class Category : u32 {
// The error category is unknown.
Unknown = 0,
IO,
// The read file doesn't follow the file format.
Format,
// Equivalent to an ASSERT(), except non-crashing.
Internal,
// The loader encountered something in the format that is not yet implemented.
Unimplemented,
};
Category category { Category::Unknown };
// Binary index: where in the file the error occurred.
size_t index { 0 };
DeprecatedFlyString description { DeprecatedString::empty() };
constexpr LoaderError() = default;
LoaderError(Category category, size_t index, DeprecatedFlyString description)
: category(category)
, index(index)
, description(move(description))
{
}
LoaderError(DeprecatedFlyString description)
: description(move(description))
{
}
LoaderError(Category category, DeprecatedFlyString description)
: category(category)
, description(move(description))
{
}
LoaderError(LoaderError&) = default;
LoaderError(LoaderError&&) = default;
LoaderError(Error&& error)
{
if (error.is_errno()) {
auto code = error.code();
description = DeprecatedString::formatted("{} ({})", strerror(code), code);
if (code == EBADF || code == EBUSY || code == EEXIST || code == EIO || code == EISDIR || code == ENOENT || code == ENOMEM || code == EPIPE)
category = Category::IO;
} else {
description = error.string_literal();
}
}
};
}
namespace AK {
template<>
struct Formatter<Audio::LoaderError> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Audio::LoaderError const& error)
{
StringView category;
switch (error.category) {
case Audio::LoaderError::Category::Unknown:
category = "Unknown"sv;
break;
case Audio::LoaderError::Category::IO:
category = "I/O"sv;
break;
case Audio::LoaderError::Category::Format:
category = "Format"sv;
break;
case Audio::LoaderError::Category::Internal:
category = "Internal"sv;
break;
case Audio::LoaderError::Category::Unimplemented:
category = "Unimplemented"sv;
break;
}
return Formatter<FormatString>::format(builder, "{} error: {} (at {})"sv, category, error.description, error.index);
}
};
}
// Convenience TRY-like macro to convert an Error to a LoaderError
#define LOADER_TRY(expression) \
({ \
auto&& _temporary_result = (expression); \
if (_temporary_result.is_error()) \
return LoaderError(_temporary_result.release_error()); \
static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
"Do not return a reference from a fallible expression"); \
_temporary_result.release_value(); \
})
|