# Buggy code
def hardlink_file(src_file, dest_file):
    """Create a hard-link (inside filesystem link) between source and destination.

    :param src_file: Source file
    :type src_file: str
    :param dest_file: Destination file
    :type dest_file: str
    """
    try:
        link(src_file, dest_file)
        fix_set_group_id(dest_file)
    except OSError as msg:
        if hasattr(msg, "errno") and msg.errno == 17:
            # File exists. Don't fallback to copy
            log.warning(
                "Failed to create hardlink of {source} at {destination}."
                " Error: {error!r}",
                {"source": src_file, "destination": dest_file, "error": msg},
            )
        else:
            log.warning(
                "Failed to create hardlink of {source} at {destination}."
                " Error: {error!r}. Copying instead",
                {
                    "source": src_file,
                    "dest": dest_file,
                    "error": msg,
                },
            )
            copy_file(src_file, dest_file)


# Traceback (KeyError)
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Quality of the episode we are processing: 720p WEB-DL
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Snatch in history: True
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Manually snatched: False
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Current quality: 720p WEB-DL
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] New quality: 720p WEB-DL
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Proper: False
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] This episode is a priority download: True
2017-05-06 19:59:19 INFO     Thread-17 :: [673d74c] New file: /media/extssd/download/chingchangchong/Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN.mkv
2017-05-06 19:59:19 INFO     Thread-17 :: [673d74c] New size: 604.70 MB
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] There is no existing file so there's no worries about replacing it
2017-05-06 19:59:19 INFO     Thread-17 :: [673d74c] This download is marked a priority download so I'm going to replace an existing file if I find one
2017-05-06 19:59:19 DEBUG    Thread-17 :: [673d74c] Trying to determine free space on destination drive
2017-05-06 19:59:20 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Tanked.S10E07.1080p.WEB-DL.AAC2.0.H.264-NTb to a series in your database. Parser result: title: Tanked, season: 10, episode: [7], screen_size: 1080p, format: WEB-DL, audio_codec: AAC, audio_channels: 2.0, video_codec: h264, release_group: NTb, type: episode, parsing_time: 0.221369028091, total_time: 0.583978891373, absolute_episode: [], quality: 1080p WEB-DL
2017-05-06 19:59:20 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Blue.Bloods.S07E22.1080p.AMZN.WEBRip.DDP5.1.x264-ViSUM to a series in your database. Parser result: title: Blue Bloods, season: 7, episode: [22], screen_size: 1080p, streaming_service: Amazon Prime, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: ViSUM, type: episode, parsing_time: 0.255120038986, total_time: 0.619963169098, absolute_episode: [], quality: 1080p WEB-DL
2017-05-06 19:59:21 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Reign.S04E11.1080p.NF.WEBRip.DD5.1.x264-ViSUM to a series in your database. Parser result: title: Reign, season: 4, episode: [11], screen_size: 1080p, streaming_service: Netflix, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: ViSUM, type: episode, parsing_time: 0.255645036697, total_time: 0.263649940491, absolute_episode: [], quality: 1080p WEB-DL
2017-05-06 19:59:21 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Reign.S04E11.720p.NF.WEBRip.DD5.1.x264-ViSUM to a series in your database. Parser result: title: Reign, season: 4, episode: [11], screen_size: 720p, streaming_service: Netflix, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: ViSUM, type: episode, parsing_time: 0.245702981949, total_time: 0.253846883774, absolute_episode: [], quality: 720p WEB-DL
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Marvels.Guardians.of.the.Galaxy.S02E11.Symbiote.War.Part.I-Wild.World.720p.DSNY.WEBRip.AAC2.0.x264-TVSmash to a series in your database. Parser result: title: Marvels Guardians of the Galaxy, season: 2, episode: [11], episode_title: Symbiote War, part: 1, screen_size: 720p, streaming_service: Disney, format: WEBRip, audio_codec: AAC, audio_channels: 2.0, video_codec: h264, video_encoder: x264, release_group: TVSmash, type: episode, parsing_time: 0.283557891846, total_time: 0.643635988235, absolute_episode: [], quality: 720p WEB-DL
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Unable to match Reign.S04E11.720p.WEB-DL.DD5.1.H.264-DRACULA to a series in your database. Parser result: title: Reign, season: 4, episode: [11], screen_size: 720p, format: WEB-DL, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, release_group: DRACULA, type: episode, parsing_time: 0.207195043564, total_time: 0.215437173843, absolute_episode: [], quality: 720p WEB-DL
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Using cached parse result for Designated.Survivor.S01E19.Misalliance.1080p.AMZN.WEBRip.DD+5.1.x264-CasStudio
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Added RSS item: Designated.Survivor.S01E19.Misalliance.1080p.AMZN.WEBRip.DD+5.1.x264-CasStudio to cache: hd4free
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Hit old cached items, not parsing any more for: hd4free
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [673d74c] Transaction with 1 queries executed
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Updating providers cache
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Search mode: RSS
2017-05-06 19:59:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] GET URL: https://freshon.tv/browse.php?incldead=0&words=0&cat=0&search= [Status: 200]
2017-05-06 19:59:24 DEBUG    Thread-17 :: [673d74c] Found release name Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN
2017-05-06 19:59:25 INFO     Thread-17 :: [673d74c] Using nzb_name for release name.
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Using cached parse result for Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Found codec for Skam Episode 4
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] 303786: Formatting pattern: %S.N.S%0SE%0E.%E.N.%RT.%SQ.N-%RG -> Skam.S04E04.Episode.4.720p.WEBRip.x264-BTN
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Using cached parse result for Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Found codec for Skam Episode 4
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] 303786: Formatting pattern: Season %0S -> Season 04
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Destination folder for this episode: /media/ssd/Shows/Skam/Season 04
2017-05-06 19:59:25 DEBUG    Thread-17 :: [673d74c] Checking if the path /media/ssd/Shows/Skam/Season 04 already exists
2017-05-06 19:59:26 INFO     TORRENTCHECKER :: [673d74c] Torrent is stopped. Keeping it: [American.Gods.S01E01.720p.WEBRip.X264-DEFLATE]
2017-05-06 19:59:26 INFO     TORRENTCHECKER :: [673d74c] Torrent not yet post-processed. Skipping: Silicon.Valley.S04E02.Terms.of.Service.720p.HBO.WEB-DL.DD5.1.H.264-monkee.mkv
2017-05-06 19:59:26 INFO     TORRENTCHECKER :: [673d74c] Torrent completed and reached minimum ratio but it was force started again. Current ratio: [0.178/0.000]. Keeping it: [Billions.S02E11.Golden.Frog.Time.720p.HULU.WEBRip.AAC2.0.H.264-Kee4DaLow.mkv]
2017-05-06 19:59:27 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] GET URL: https://freshon.tv/browse.php?incldead=0&words=0&cat=0&search=&page=1 [Status: 200]
2017-05-06 19:59:28 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] GET URL: https://freshon.tv/browse.php?incldead=0&words=0&cat=0&search=&page=2 [Status: 200]
2017-05-06 19:59:37 INFO     TORRENTCHECKER :: [673d74c] Torrent not yet post-processed. Skipping: Say.Yes.to.the.Dress.S15E07.We.Dont.Do.Anything.Normal.1080p.WEBRip.x264-WEBSTER
2017-05-06 19:59:38 INFO     TORRENTCHECKER :: [673d74c] Torrent not yet post-processed. Skipping: Silicon.Valley.S04E01.1080p.WEBRip.X264-DEFLATE.mkv
2017-05-06 19:59:38 INFO     SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Removing cache entries older than 30 days from freshontv
2017-05-06 19:59:39 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Unable to match Underground.S02E09.720p.AMZN.WEBRip.DD+5.1.x264-RTN to a series in your database. Parser result: title: Underground, season: 2, episode: [9], screen_size: 720p, streaming_service: Amazon Prime, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: RTN, type: episode, parsing_time: 0.256384849548, total_time: 0.623630046844, absolute_episode: [], quality: 720p WEB-DL
2017-05-06 19:59:39 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Unable to match Blue.Bloods.S07E22.REPACK.1080p.AMZN.WEBRip.DD+5.1.x264-ViSUM to a series in your database. Parser result: title: Blue Bloods, season: 7, episode: [22], other: Proper, proper_count: 1, proper_tag: REPACK, screen_size: 1080p, streaming_service: Amazon Prime, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: ViSUM, type: episode, parsing_time: 0.27284693718, total_time: 0.638719081879, absolute_episode: [], quality: 1080p WEB-DL
2017-05-06 19:59:39 INFO     TORRENTCHECKER :: [673d74c] Torrent did not reach minimum ratio: [0.290/2.000]. Keeping it: [Say.Yes.To.The.Dress.S15E08.My.Sisters.Are.Picking.Out.My.Dress.HDTV.x264-[NY2] -]
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Unable to match TNA.PPV.Pack.2005.720p.WEB-DL.x264.Fight-BB to a series in your database. Parser result: title: TNA, year: 2005, screen_size: 720p, format: WEB-DL, video_codec: h264, video_encoder: x264, release_group: Fight, type: episode, parsing_time: 0.19260597229, total_time: 0.563721895218, absolute_episode: [], quality: 720p WEB-DL, episode: [], season: None
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Using cached parse result for Rogue.S04E07.1080p.WEBRip.X264-DEFLATE
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Added RSS item: Rogue.S04E07.1080p.WEBRip.X264-DEFLATE to cache: freshontv
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Unable to match Reign.S04E11.1080p.WEB-DL.DD5.1.H.264-DRACULA to a series in your database. Parser result: title: Reign, season: 4, episode: [11], screen_size: 1080p, format: WEB-DL, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, release_group: DRACULA, type: episode, parsing_time: 0.224695920944, total_time: 0.233439207077, absolute_episode: [], quality: 1080p WEB-DL
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Hit old cached items, not parsing any more for: freshontv
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [FreshOnTV] :: [673d74c] Transaction with 1 queries executed
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] Updating providers cache
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] POST URL: https://api.t411.ai/auth [Status: 403]
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] With post data: username=**********&password=**********
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] Requested url https://api.t411.ai/auth returned status code 403: Forbidden
2017-05-06 19:59:40 WARNING  SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] Unable to connect to provider
2017-05-06 19:59:40 INFO     SEARCHQUEUE-DAILY-SEARCH :: [T411] :: [673d74c] Removing cache entries older than 30 days from t411
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [673d74c] Last update was too soon, using old cache: 1494096509. Updated less then 20 minutes ago
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Updating providers cache
2017-05-06 19:59:40 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Search mode: RSS
2017-05-06 19:59:41 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] GET URL: http://173.8.196.73:8282/nnp/api?apikey=**********&maxage=3000&cat=5000%2C5080%2C5020%2C5040%2C5050%2C5030%2C5060&limit=100&t=search&offset=0 [Status: 200]
2017-05-06 19:59:43 INFO     SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Removing cache entries older than 30 days from nutech
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Using cached parse result for Sense8.S02E07.1080p.WEBRip.x264-MOROSE
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Added RSS item: Sense8.S02E07.1080p.WEBRip.x264-MOROSE to cache: nutech
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Unable to match Married.at.First.Sight.Second.Chances.S01E02.WEB.h264-CookieMonster to a series in your database. Parser result: title: Married at First Sight Second Chances, season: 1, episode: [2], format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 0.164175033569, total_time: 0.543497085571, absolute_episode: [], quality: SDTV
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Hit old cached items, not parsing any more for: nutech
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Nutech] :: [673d74c] Transaction with 1 queries executed
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Pretome] :: [673d74c] Updating providers cache
2017-05-06 19:59:44 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Pretome] :: [673d74c] Search mode: RSS
2017-05-06 19:59:46 DEBUG    Thread-17 :: [673d74c] No associated files were found for /media/extssd/download/chingchangchong/Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN.mkv
2017-05-06 19:59:46 DEBUG    Thread-17 :: [673d74c] Hard linking file from /media/extssd/download/chingchangchong/Skam.S04E04.720p.YLE.WEBRip.AAC2.0.x264-BTN.mkv to /media/ssd/Shows/Skam/Season 04/Skam.S04E04.Episode.4.720p.WEBRip.x264-BTN.mkv
2017-05-06 19:59:46 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Pretome] :: [673d74c] GET URL: https://pretome.info/browse.php?search=&cat%5B%5D=7&st=1 [Status: 200]
2017-05-06 19:59:46 ERROR    Thread-17 :: [673d74c] BraceMessage string formatting failed. Using representation instead.
Traceback (most recent call last):
  File "/home/pi/Medusa/medusa/logger/adapters/style.py", line 34, in __str__
    return self.msg.format(*args, **kwargs)
KeyError: u'destination'
2017-05-06 19:59:47 ERROR    Thread-17 :: [673d74c] BraceMessage string formatting failed. Using representation instead.
Traceback (most recent call last):
  File "/home/pi/Medusa/medusa/logger/adapters/style.py", line 34, in __str__
    return self.msg.format(*args, **kwargs)
KeyError: u'destination'

# Fixed code
def hardlink_file(src_file, dest_file):
    """Create a hard-link (inside filesystem link) between source and destination.

    :param src_file: Source file
    :type src_file: str
    :param dest_file: Destination file
    :type dest_file: str
    """
    try:
        link(src_file, dest_file)
        fix_set_group_id(dest_file)
    except OSError as msg:
        if hasattr(msg, "errno") and msg.errno == 17:
            # File exists. Don't fallback to copy
            log.warning(
                "Failed to create hardlink of {source} at {destination}."
                " Error: {error!r}",
                {"source": src_file, "destination": dest_file, "error": msg},
            )
        else:
            log.warning(
                "Failed to create hardlink of {source} at {destination}."
                " Error: {error!r}. Copying instead",
                {
                    "source": src_file,
                    "destination": dest_file,
                    "error": msg,
                },
            )
            copy_file(src_file, dest_file)