import traceback


def get_last_frame(tb):
    """ Get frame that must be showed to user. In some case it's not the
    last stack frame, ex: abort(404)
     """
    frames = traceback.extract_tb(tb)
    if not frames:
        return None

    target_frame = frames[-1]
    for frame in frames[::-1]:
        # ignore stack from installed and std packages
        if 'site-packages' in frame.filename or '/synchrolog_flask' in frame.filename:
            continue
        target_frame = frame
        break
    return target_frame