2 ๋ถ„ ์†Œ์š”

try ~ except ์ ˆ์„ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋กœ ๊ด€๋ฆฌํ•ด๋ณด์ž!

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๋„์ค‘, ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋กœ์ง์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๊ด€๋ฆฌํ•˜๋ฉด view(or controller)์—์„œ ๋ฐ˜๋ณต์ ์ธ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋กœ์ง์ด ํ•„์š” ์—†์–ด์ ธ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์งˆ๊ฑฐ ๊ฐ™๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๋‹ค!

๊ทธ๋ž˜์„œ django์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด, ํ•ด๋‹น ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค!

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š”๊ฑด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

์šฐ์„ , django์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ณด์ž!

from functools import wraps

def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        # ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ํ•˜๋Š” ์ผ
        result = func(*args, **kwargs)
        return result
    return wrapper

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด, ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ @my_decorator ๋ฅผ ์ ์–ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@my_decorator
def say_hello(name):
    return f"Hello, {name}!"
   
   
print(say_hello("Alice"))  # "Calling function say_hello"์™€ "Hello, Alice!" ์ถœ๋ ฅ
print(say_hello.__name__)  # "say_hello" ์ถœ๋ ฅ
print(say_hello.__doc__)   # "Greet someone with their name." ์ถœ๋ ฅ

๋‚ด๊ฐ€ ์ ์–ด๋ณธ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ!

def exception_handler(view: bool = False):
    def exception_handler_decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except Card.DoesNotExist as e:
                print(f"An error occurred in {func.__name__}: {e}")
                return error_response("Invalid Card", status.HTTP_400_BAD_REQUEST)
            except Game.DoesNotExist as e:
                print(f"An error occuerd in {func.__name__}: {e}")
                return error_response("Invalid Game", status.HTTP_400_BAD_REQUEST)
            except Gametype.DoesNotExist as e:
                print(f"An error occurred in {func.__name__}: {e}")
                return error_response("Invalid Game Type", status.HTTP_400_BAD_REQUEST)
            except AIAnswer.DoesNotExist as e:
                print(f"An error occurred in {func.__name__}: {e}")
                return error_response("Invalid AI Answer", status.HTTP_400_BAD_REQUEST)
            except Exception as e:
                print(f"An error occurred in {func.__name__}: {e}")
                return error_response("Internal Server Error", status.HTTP_500_INTERNAL_SERVER_ERROR)
        return wrapper
    return exception_handler_decorator
  • def exception_handler(view: bool = False):
    • views์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ •์˜ํ•œ๋‹ค.
    • view: bool = False: ๋งŒ์•ฝ ์ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ function์ด view์ธ์ง€ ์•„๋‹Œ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ธฐ๋ณธ ๊ฐ’์€ False
  • @wraps(func)
    • Python์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ๋กœ, functools ๋ชจ๋“ˆ์—์„œ ์ œ๊ณต๋œ๋‹ค.
    • ๋ฐ์ฝ”๋ ˆ์ดํŠธ๋œ ํ•จ์ˆ˜๊ฐ€ ์›๋ž˜ ํ•จ์ˆ˜์˜ ์ด๋ฆ„, ๋ฌธ์„œ ๋ฌธ์ž์—ด(docstring), ์ฃผ์„, ๋ฐ ๊ธฐํƒ€ ์†์„ฑ์„ ์žƒ์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.
  • def wrapper(*args, **kwargs)
    • wrapper ํ•จ์ˆ˜๋Š” ์›๋ž˜ ํ•จ์ˆ˜(func)๋ฅผ ๋ž˜ํ•‘ํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • *args์™€ **kwargs๋Š” ์›๋ž˜ ํ•จ์ˆ˜์˜ ๋ชจ๋“  ์ธ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ›๋Š”๋‹ค.
    • ๋ณธ๊ฒฉ์ ์ธ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • try ~ except
    • return func(*args, **kwargs): ์›๋ž˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ์ดํ›„์—๋Š” ๋ฐ์ดํ„ฐ ์กด์žฌ ์—ฌ๋ถ€์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์™€ ๊ทธ ์™ธ์˜ ์˜ˆ์™ธ๋Š” ์ œ๋„ค๋ฆญํ•˜๊ฒŒ ๋ฐ›์•„ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๋‚ด๊ฐ€ ๋งŒ๋“  ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์—์„œ ์“ด error_response๋Š” ๋ญ์ง€?

from rest_framework import status
from rest_framework.response import Response

def error_response(error: str, status_code: int):
    return Response({"error": error}, status_code)
  • ์ธ์ž๋กœ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€(error)์™€ HTTP ์ƒํƒœ ์ฝ”๋“œ(status_code)๋ฅผ ๋ฐ›๋Š”๋‹ค.
  • ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” {"error": error} ํฌ๋ฉง์˜ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋™์ž‘ ์ˆœ์„œ?

ํ•˜๋‚˜์˜ ๋™์ž‘(ํ•จ์ˆ˜)์„ ์ˆ˜ํ–‰ํ•  ๋•Œ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฉด ์ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋“ค์ด ์–ด๋–ค ์ˆœ์„œ๋ฅผ ๊ฐ–๊ณ  ๋™์ž‘ํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„์•ผ ๋‚ด๊ฐ€ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ์ ์ ˆํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค!

KakaoTalk_Photo_2024-06-13-03-45-18

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค!

ํ•จ์ˆ˜๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๊ณ , ์œ„์— ์žˆ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ โ†’ ๊ทธ ์œ„์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ˆœ์œผ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.

์ด ํฌ์ŠคํŒ…์—์„œ ์ž‘์„ฑํ•œ ์˜ˆ์‹œ๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์˜€์œผ๋‹ˆ, ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ”๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋ฏ€๋กœ, ํ•จ์ˆ˜ ๋ฐ”๋กœ ์œ„์— ์„ ์–ธํ–ˆ๋‹ค.

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ