-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |