Skip to content

Commit

Permalink
update response (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
taogeYT authored Dec 24, 2024
1 parent bb5357c commit 74f404d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions appboot/response.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from typing import Generic, Optional, TypeVar

from pydantic import BaseModel
from fastapi import HTTPException

from appboot import Schema
from appboot.exceptions import Error

T = TypeVar('T')


class APIResponse(BaseModel, Generic[T]):
class APIResponse(Schema, Generic[T]):
code: int = 200
message: str = 'success'
data: Optional[T] = None

@classmethod
def from_exception(cls, e: Exception) -> 'APIResponse':
if isinstance(e, HTTPException):
return cls(code=e.status_code, message=e.detail)
if isinstance(e, Error):
return cls(code=e.code, message=str(e))
return cls(code=500, message=str(e))

0 comments on commit 74f404d

Please sign in to comment.