devicetypeAPIGET1
This commit is contained in:
9
backend/common/constants/status_code.py
Normal file
9
backend/common/constants/status_code.py
Normal file
@@ -0,0 +1,9 @@
|
||||
HTTP_OK = 200
|
||||
HTTP_CREATED = 201
|
||||
HTTP_BAD_REQUEST = 400
|
||||
HTTP_UNAUTHORIZED = 401
|
||||
HTTP_FORBIDDEN = 403
|
||||
HTTP_NOT_FOUND = 404
|
||||
HTTP_CONFLICT = 409
|
||||
HTTP_UNPROCESSABLE_ENTITY = 422
|
||||
HTTP_INTERNAL_SERVER_ERROR = 500
|
||||
62
backend/common/exceptions/app_exception.py
Normal file
62
backend/common/exceptions/app_exception.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# Đây là exception chung cho toàn bộ dự án
|
||||
class AppException(Exception): # Tạo ra class AppException kế thừa lại Exception của thư viện
|
||||
def __init__(
|
||||
self,
|
||||
message,
|
||||
status_code=400,
|
||||
error_code="BAD_REQUEST",
|
||||
payload=None
|
||||
):
|
||||
super().__init__(message) #kế thừa thêm thuộc tính
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
self.error_code = error_code
|
||||
self.payload = payload or {}
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"success": False,
|
||||
"error": self.error_code,
|
||||
"message": self.message,
|
||||
"details": self.payload
|
||||
}
|
||||
|
||||
# Rồi từ thằng kế thừa lại kế thừa tiếp từ thằng AppException
|
||||
class BadRequestException(AppException):
|
||||
def __init__(self, message="Bad request", payload=None):
|
||||
super().__init__(
|
||||
message=message,
|
||||
status_code=400,
|
||||
error_code="BAD_REQUEST",
|
||||
payload=payload
|
||||
)
|
||||
|
||||
|
||||
class NotFoundException(AppException):
|
||||
def __init__(self, message="Resource not found", payload=None):
|
||||
super().__init__(
|
||||
message=message,
|
||||
status_code=404,
|
||||
error_code="NOT_FOUND",
|
||||
payload=payload
|
||||
)
|
||||
|
||||
|
||||
class ConflictException(AppException):
|
||||
def __init__(self, message="Resource already exists", payload=None):
|
||||
super().__init__(
|
||||
message=message,
|
||||
status_code=409,
|
||||
error_code="CONFLICT",
|
||||
payload=payload
|
||||
)
|
||||
|
||||
|
||||
class UnprocessableEntityException(AppException):
|
||||
def __init__(self, message="Unprocessable entity", payload=None):
|
||||
super().__init__(
|
||||
message=message,
|
||||
status_code=422,
|
||||
error_code="UNPROCESSABLE_ENTITY",
|
||||
payload=payload
|
||||
)
|
||||
50
backend/common/exceptions/handler.py
Normal file
50
backend/common/exceptions/handler.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from marshmallow import ValidationError
|
||||
|
||||
from common.exceptions.app_exception import AppException
|
||||
from common.response.api_response import error_response
|
||||
from common.constants.status_code import (
|
||||
HTTP_BAD_REQUEST,
|
||||
HTTP_NOT_FOUND,
|
||||
HTTP_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
# Phải tạo global error handler(ở project cũ là để phần này ra app.py nhưng bây giờ chỉ cần tạo file hander rồi đăng kí ở app.py để nó thành global bắt lỗi toàn dự án)
|
||||
# Ngoài ra để có được các lỗi của toàn dự án, thì phải custom exception theo dự án bằng cách kế thừa lại Exception để custom thêm thuộc tính mới
|
||||
|
||||
def register_error_handlers(app): # Đăng ký hàm này ở app.py để nó bắt lỗi toàn dự án
|
||||
|
||||
@app.errorhandler(AppException)
|
||||
def handle_app_exception(error):
|
||||
return error_response(
|
||||
message=error.message,
|
||||
error=error.error_code,
|
||||
status_code=error.status_code,
|
||||
details=getattr(error, "payload", None)
|
||||
)
|
||||
|
||||
|
||||
@app.errorhandler(ValidationError)
|
||||
def handle_validation_error(error):
|
||||
return error_response(
|
||||
message="Invalid request data",
|
||||
error="VALIDATION_ERROR",
|
||||
status_code=HTTP_BAD_REQUEST,
|
||||
details=error.messages
|
||||
)
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def handle_not_found(error):
|
||||
return error_response(
|
||||
message="Route not found",
|
||||
error="NOT_FOUND",
|
||||
status_code=HTTP_NOT_FOUND
|
||||
)
|
||||
|
||||
|
||||
@app.errorhandler(500)
|
||||
def handle_internal_error(error):
|
||||
return error_response(
|
||||
message="Internal server error",
|
||||
error="INTERNAL_SERVER_ERROR",
|
||||
status_code=HTTP_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
28
backend/common/response/api_response.py
Normal file
28
backend/common/response/api_response.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from flask import jsonify
|
||||
from common.constants.status_code import HTTP_OK, HTTP_BAD_REQUEST
|
||||
|
||||
|
||||
def success_response(
|
||||
data=None,
|
||||
message="Success",
|
||||
status_code=HTTP_OK
|
||||
):
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": message,
|
||||
"data": data
|
||||
}), status_code
|
||||
|
||||
|
||||
def error_response(
|
||||
message="Error",
|
||||
error="ERROR",
|
||||
status_code=HTTP_BAD_REQUEST,
|
||||
details=None
|
||||
):
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": error,
|
||||
"message": message,
|
||||
"details": details
|
||||
}), status_code
|
||||
Reference in New Issue
Block a user