Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yingtung authored Feb 17, 2024
1 parent fab9832 commit 46a20e0
Showing 1 changed file with 0 additions and 98 deletions.
98 changes: 0 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,104 +20,6 @@ A library designed to host common components for a cluster of microservices shar

1. `pip install awesome-sso`

### Exceptions

Using fast API as example, we may simply throw exception with a proper status code, and an optional error code. We may
also supply arbitrary key value in args dict, to help frontend render better error message.

```python
from awesome_sso.exceptions import NotFound
from fastapi import APIRouter

router = APIRouter()


@router.get('/transactions')
def get(id: str):
try:
obj = find_by_id(id)
except Exception as e:
raise NotFound(message='transaction not found' % id, error_code='A0001', args={id: id})
...
```

And we may implement a common error handler to convert all these errors to proper response schema

```python
from awesome_sso.exceptions import HTTPException
from fastapi.requests import Request
from fastapi.responses import JSONResponse


@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
'detail': exc.detail,
'error_code': exc.error_code,
}
)
```

This would result in a response with status code 404, and body

```json
{
"status_code": 404,
"detail": {
"message": "transaction not found",
"id": "some_id"
},
"error_code": "A0001"
}
```

With this response, frontend can decide to simply render detail, or map it to detailed message. If error_code "A0001"
correspond to the following i18 n entry

```json
"error.A0001": {"en-US": "transaction can not be found with supplied {id}: {message}"}
```

we may format message accordingly with

```typescript
errorMessage = formatMessage({ id: `error.${error.data.error_code}` }, error.data.detail);
```

Note that error code is not supplied, is default to status code. So it is always safe to simply use error_code in
frontend to decide what to render.

### Data Store

#### Minio

refer to `tests/test_minio.py`

#### Mongo

refer to `tests/service/test_user.py`

```python
from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from awesome_sso.service.user.schema import AwesomeUser


def init_mongo():
settings = YOUR_SETTINGS()
models = [AwesomeUser]
cli = AsyncIOMotorClient(settings.mongodb_dsn)
await init_beanie(
database=cli[settings.mongodb_db_name],
document_models=models,
)
for model in models:
await model.get_motor_collection().drop()
await model.get_motor_collection().drop_indexes()
```

### Service

#### configure service settings
Expand Down

0 comments on commit 46a20e0

Please sign in to comment.