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
Notes: sideshowbarker 2024-07-17 22:33:03 +09:00
3 changed files with 12 additions and 6 deletions

View file

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

View file

@ -18,6 +18,7 @@
#ifndef KERNEL
# include <stdio.h>
# include <string.h>
#endif
namespace AK {
@ -618,9 +619,18 @@ template<>
struct Formatter<Error> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Error const& error)
{
#if defined(__serenity__) && defined(KERNEL)
if (error.is_errno())
return Formatter<FormatString>::format(builder, "Error(errno={})", error.code());
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()) {
auto error = result.release_error();
if (error.is_syscall())
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());
warnln("Runtime error: {}", error);
return 1;
}
return result.value();