Skip to content

Instantly share code, notes, and snippets.

@peketamin
Last active November 9, 2018 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peketamin/2e25dc187055a9183c59e19bc3129ca4 to your computer and use it in GitHub Desktop.
Save peketamin/2e25dc187055a9183c59e19bc3129ca4 to your computer and use it in GitHub Desktop.
Example of Exception handling
def greeting(user_id):
    """Parent func"""
    try:
        user_name = get_user_name(user_id)
    except User.DoesNotExists as e:
        logger.error("[greeting] user: {user_id} not found.")
        return None
    print(f"Hello, {user_name}")


def get_user_name(user_id):
    """Child func"""
    user = User.get(user_id)
    return user.name
  • スタックトレース出しまくるとエラーログが見づらくなるを防げる
  • どのデータでエラーが起きているのかのヒントが得られる
  • その関数でどんな例外が起こり得るのかが第三者の改修担当から見ても読み取れる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment