# Buggy code
def set_attributes_if_different(
    self, path, attributes, changed, diff=None, expand=True
):
    if attributes is None:
        return changed

    b_path = to_bytes(path, errors="surrogate_or_strict")
    if expand:
        b_path = os.path.expanduser(os.path.expandvars(b_path))
    path = to_text(b_path, errors="surrogate_or_strict")

    existing = self.get_file_attributes(b_path)

    if existing.get("attr_flags", "") != attributes:
        attrcmd = self.get_bin_path("chattr")
        if attrcmd:
            attrcmd = [attrcmd, "=%s" % attributes, b_path]
            changed = True

            if diff is not None:
                if "before" not in diff:
                    diff["before"] = {}
                diff["before"]["attributes"] = existing.get("attr_flags")
                if "after" not in diff:
                    diff["after"] = {}
                diff["after"]["attributes"] = attributes

            if not self.check_mode:
                try:
                    rc, out, err = self.run_command(attrcmd)
                    if rc != 0 or err:
                        raise Exception(
                            "Error while setting attributes: %s" % (out + err)
                        )
                except:
                    e = get_exception()
                    self.fail_json(path=path, msg="chattr failed", details=str(e))
    return changed


# Traceback (LookupError)
exception:
Traceback (most recent call last):
File "/tmp/ansible_atAUEE/ansible_module_file.py", line 480, in <module>
main()
File "/tmp/ansible_atAUEE/ansible_module_file.py", line 362, in main
changed |= recursive_set_attributes(module, to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args)
File "/tmp/ansible_atAUEE/ansible_module_file.py", line 171, in recursive_set_attributes
changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)
File "/tmp/ansible_atAUEE/ansible_modlib.zip/ansible/module_utils/basic.py", line 1292, in set_fs_attributes_if_different
File "/tmp/ansible_atAUEE/ansible_modlib.zip/ansible/module_utils/basic.py", line 995, in set_owner_if_different
File "/tmp/ansible_atAUEE/ansible_modlib.zip/ansible/module_utils/_text.py", line 232, in to_text
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
LookupError: unknown error handler name 'surrogate_then_strict'

# Fixed code
def set_attributes_if_different(
    self, path, attributes, changed, diff=None, expand=True
):
    if attributes is None:
        return changed

    b_path = to_bytes(path, errors="surrogate_or_strict")
    if expand:
        b_path = os.path.expanduser(os.path.expandvars(b_path))

    existing = self.get_file_attributes(b_path)

    if existing.get("attr_flags", "") != attributes:
        attrcmd = self.get_bin_path("chattr")
        if attrcmd:
            attrcmd = [attrcmd, "=%s" % attributes, b_path]
            changed = True

            if diff is not None:
                if "before" not in diff:
                    diff["before"] = {}
                diff["before"]["attributes"] = existing.get("attr_flags")
                if "after" not in diff:
                    diff["after"] = {}
                diff["after"]["attributes"] = attributes

            if not self.check_mode:
                try:
                    rc, out, err = self.run_command(attrcmd)
                    if rc != 0 or err:
                        raise Exception(
                            "Error while setting attributes: %s" % (out + err)
                        )
                except:
                    e = get_exception()
                    path = to_text(b_path)
                    self.fail_json(path=path, msg="chattr failed", details=str(e))
    return changed