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