Passed
Push — master ( 99f654...da6e57 )
by Mingyu
01:48 queued 52s
created

{{cookiecutter.project_slug}}.app.hooks.error   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 19
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A broad_exception_handler() 0 21 4
1
from http import HTTPStatus
2
3
from flask import current_app, jsonify
4
from pydantic import ValidationError
5
from werkzeug.exceptions import HTTPException
6
7
8
def broad_exception_handler(e: Exception):
9
    # TODO 에러를 세분화해서 잡는 것을 추천합니다.
10
11
    if isinstance(e, HTTPException):
12
        message = e.description
13
        code = e.code
14
15
    elif isinstance(e, ValidationError):
16
        message = e.errors()
17
        code = HTTPStatus.BAD_REQUEST
18
19
    else:
20
        message = ""
21
        code = HTTPStatus.INTERNAL_SERVER_ERROR
22
23
        if current_app.debug:
24
            import traceback
25
26
            traceback.print_exc()
27
28
    return jsonify({"error": message}), code
29