Repository: fmtlib/fmt
File: src/os.cc (C++)

Code before:
      if (utf8_message.convert(msg.rtrim()) == ERROR_SUCCESS) {
        return utf8_message.str();
      }
    }
    return "unknown error";
  }
};

}  // namespace detail

FMT_API const std::error_category& system_category() FMT_NOEXCEPT {
  static const detail::utf8_system_category category;
  return category;
}

std::system_error vwindows_error(int err_code, string_view format_str,
                                 format_args args) {
  auto ec = std::error_code(err_code, system_category());
  throw std::system_error(ec, vformat(format_str, args));
}

void detail::format_windows_error(detail::buffer<char>& out, int error_code,
                                  const char* message) FMT_NOEXCEPT {
  FMT_TRY {
    system_message msg(error_code);
    if (msg.result() != 0) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg) == ERROR_SUCCESS) {
        format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message);
        return;
      }
    }
  }
  FMT_CATCH(...) {}
  format_error_code(out, error_code, message);
}

void report_windows_error(int error_code, const char* message) FMT_NOEXCEPT {
  report_error(detail::format_windows_error, error_code, message);
}
#else
const std::error_category& system_category() FMT_NOEXCEPT {
  return std::system_category();
}
#endif  // _WIN32

buffered_file::~buffered_file() FMT_NOEXCEPT {
  if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
    report_system_error(errno, "cannot close file");
}


Review comment (suggestion):
Same here:

```c++
if (system_message msg(error_code)) {
```

Code after:
        return utf8_message.str();
      }
    }
    return "unknown error";
  }
};

}  // namespace detail

FMT_API const std::error_category& system_category() FMT_NOEXCEPT {
  static const detail::utf8_system_category category;
  return category;
}

std::system_error vwindows_error(int err_code, string_view format_str,
                                 format_args args) {
  auto ec = std::error_code(err_code, system_category());
  throw std::system_error(ec, vformat(format_str, args));
}

void detail::format_windows_error(detail::buffer<char>& out, int error_code,
                                  const char* message) FMT_NOEXCEPT {
  FMT_TRY {
    system_message msg(error_code);
    if (msg) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg) == ERROR_SUCCESS) {
        format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message);
        return;
      }
    }
  }
  FMT_CATCH(...) {}
  format_error_code(out, error_code, message);
}

void report_windows_error(int error_code, const char* message) FMT_NOEXCEPT {
  report_error(detail::format_windows_error, error_code, message);
}
#endif  // _WIN32

buffered_file::~buffered_file() FMT_NOEXCEPT {
  if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
    report_system_error(errno, "cannot close file");
}

buffered_file::buffered_file(cstring_view filename, cstring_view mode) {
  FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())),
                nullptr);
  if (!file_)
    FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str()));