# Buggy code
def api_method(f):
    def wrapper(api, *args, **kwargs):
        quiet = kwargs.pop("quiet", False)
        old_curdir = get_cwd()
        old_output = api.user_io.out
        quiet_output = ConanOutput(StringIO(), api.color) if quiet else None
        try:
            api.create_app(quiet_output=quiet_output)
            log_command(f.__name__, kwargs)
            with environment_append(api.app.cache.config.env_vars):
                return f(api, *args, **kwargs)
        except Exception as exc:
            if quiet_output:
                old_output.write(quiet_output._stream.getvalue())
                old_output.flush()
            msg = exception_message_safe(exc)
            try:
                log_exception(exc, msg)
            except BaseException:
                pass
            raise
        finally:
            os.chdir(old_curdir)

    return wrapper


# Traceback (AttributeError)
$ rm -rf .conan\remotes.json

$ conan inspect zlib/1.2.11@ --raw=version
Traceback (most recent call last):
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\command.py", line 1969, in run
method(args[0][1:])
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\command.py", line 255, in inspect
result = self._conan.inspect(args.path_or_reference, attributes, args.remote, quiet=quiet)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\conan_api.py", line 81, in wrapper
return f(api, *args, **kwargs)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\conan_api.py", line 260, in inspect
remotes = self.app.load_remotes(remote_name=remote_name)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\conan_api.py", line 198, in load_remotes
remotes = self.cache.registry.load_remotes()
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\cache\remote_registry.py", line 313, in load_remotes
"creating default one in %s" % self._filename)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\output.py", line 121, in warn
self.writeln("WARN: {}".format(data), Color.BRIGHT_YELLOW, error=True)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\output.py", line 74, in writeln
self.write(data, front, back, newline=True, error=error)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\output.py", line 99, in write
self._write_err(data, newline)
File "c:\users\memsharded\envs\conanpip37\lib\site-packages\conans\client\output.py", line 84, in _write_err
self._stream_err.write(data)
AttributeError: 'bool' object has no attribute 'write'

ERROR: 'bool' object has no attribute 'write'

# Fixed code
def api_method(f):
    def wrapper(api, *args, **kwargs):
        quiet = kwargs.pop("quiet", False)
        old_curdir = get_cwd()
        old_output = api.user_io.out
        quiet_output = ConanOutput(StringIO(), color=api.color) if quiet else None
        try:
            api.create_app(quiet_output=quiet_output)
            log_command(f.__name__, kwargs)
            with environment_append(api.app.cache.config.env_vars):
                return f(api, *args, **kwargs)
        except Exception as exc:
            if quiet_output:
                old_output.write(quiet_output._stream.getvalue())
                old_output.flush()
            msg = exception_message_safe(exc)
            try:
                log_exception(exc, msg)
            except BaseException:
                pass
            raise
        finally:
            os.chdir(old_curdir)

    return wrapper