1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

AK+LibMain: Improve formatter for AK::Error in userspace

Print the full associated string metadata by default (if available.)
This commit is contained in:
Andreas Kling 2021-12-20 11:55:32 +01:00
parent 8877dd0eea
commit 452a5531be
3 changed files with 12 additions and 6 deletions

View file

@ -15,6 +15,7 @@
# include <LibC/errno_codes.h> # include <LibC/errno_codes.h>
#else #else
# include <errno.h> # include <errno.h>
# include <string.h>
#endif #endif
namespace AK { namespace AK {

View file

@ -18,6 +18,7 @@
#ifndef KERNEL #ifndef KERNEL
# include <stdio.h> # include <stdio.h>
# include <string.h>
#endif #endif
namespace AK { namespace AK {
@ -618,9 +619,18 @@ template<>
struct Formatter<Error> : Formatter<FormatString> { struct Formatter<Error> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Error const& error) ErrorOr<void> format(FormatBuilder& builder, Error const& error)
{ {
#if defined(__serenity__) && defined(KERNEL)
if (error.is_errno()) if (error.is_errno())
return Formatter<FormatString>::format(builder, "Error(errno={})", error.code()); return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
return Formatter<FormatString>::format(builder, "Error({})", error.string_literal()); return Formatter<FormatString>::format(builder, "Error({})", error.string_literal());
#else
if (error.is_syscall())
return Formatter<FormatString>::format(builder, "{}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
if (error.is_errno())
return Formatter<FormatString>::format(builder, "{} (errno={})", strerror(error.code()), error.code());
return Formatter<FormatString>::format(builder, "{}", error.string_literal());
#endif
} }
}; };

View file

@ -24,12 +24,7 @@ int main(int argc, char** argv)
}); });
if (result.is_error()) { if (result.is_error()) {
auto error = result.release_error(); auto error = result.release_error();
if (error.is_syscall()) warnln("Runtime error: {}", error);
warnln("Runtime error: {}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
else if (error.is_errno())
warnln("Runtime error: {} (errno={})", strerror(error.code()), error.code());
else
warnln("Runtime error: {}", error.string_literal());
return 1; return 1;
} }
return result.value(); return result.value();