-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a new default application exception handler
- Loading branch information
Showing
7 changed files
with
148 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from pydantic import BaseModel | ||
|
||
from esmerald import Esmerald, Gateway, JSONResponse, post | ||
|
||
|
||
class DataIn(BaseModel): | ||
id: int | ||
name: str | ||
|
||
|
||
@post("/create") | ||
async def create(data: DataIn) -> JSONResponse: | ||
# Simple validation to raise ValueError | ||
if data.id > 20: | ||
raise ValueError("The ID must be less than 20.") | ||
|
||
|
||
app = Esmerald(routes=[Gateway(handler=create)]) |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from esmerald import Esmerald, Gateway, JSONResponse, post | ||
from esmerald.exception_handlers import pydantic_validation_error_handler, value_error_handler | ||
|
||
|
||
class DataIn(BaseModel): | ||
id: int | ||
name: str | ||
|
||
|
||
@post("/create") | ||
async def create(data: DataIn) -> JSONResponse: | ||
# Simple validation to raise ValueError | ||
if data.id > 20: | ||
raise ValueError("The ID must be less than 20.") | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=create)], | ||
exception_handlers={ | ||
ValueError: value_error_handler, | ||
}, | ||
) |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from esmerald import Esmerald, Gateway, JSONResponse, post | ||
from esmerald.exception_handlers import pydantic_validation_error_handler, value_error_handler | ||
|
||
|
||
class DataIn(BaseModel): | ||
id: int | ||
name: str | ||
|
||
|
||
@post("/create") | ||
async def create(data: DataIn) -> JSONResponse: | ||
# Simple validation to raise ValueError | ||
if data.id > 20: | ||
raise ValueError("The ID must be less than 20.") | ||
|
||
|
||
app = Esmerald( | ||
routes=[ | ||
Gateway( | ||
handler=create, | ||
exception_handlers={ | ||
ValueError: value_error_handler, | ||
}, | ||
) | ||
], | ||
) |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from esmerald import Esmerald, Gateway, JSONResponse, post | ||
from esmerald.exception_handlers import pydantic_validation_error_handler, value_error_handler | ||
|
||
|
||
class DataIn(BaseModel): | ||
id: int | ||
name: str | ||
|
||
|
||
@post( | ||
"/create", | ||
exception_handlers={ | ||
ValueError: value_error_handler, | ||
}, | ||
) | ||
async def create(data: DataIn) -> JSONResponse: | ||
# Simple validation to raise ValueError | ||
if data.id > 20: | ||
raise ValueError("The ID must be less than 20.") | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=create)], | ||
) |
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
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 |
---|---|---|
|
@@ -34,12 +34,12 @@ async def update() -> JSON: | |
DataIn(name="Esmerald", email="[email protected]") | ||
|
||
|
||
def test_pydantic_validation_error_handler_return_500(test_client_factory): | ||
def test_pydantic_validation_error_handler_return_422(test_client_factory): | ||
with create_client( | ||
routes=[Gateway(handler=create)], | ||
) as client: | ||
response = client.post("/create") | ||
assert response.status_code == 500 | ||
assert response.status_code == 422 | ||
|
||
|
||
def test_pydantic_validation_error_handler(test_client_factory): | ||
|
@@ -64,33 +64,12 @@ def test_pydantic_validation_error_handler(test_client_factory): | |
assert "email" in locs | ||
|
||
|
||
def test_value_error_handler_return_500(test_client_factory): | ||
with create_client( | ||
routes=[Gateway(handler=update)], | ||
) as client: | ||
response = client.put("/update") | ||
assert response.status_code == 500 | ||
|
||
|
||
def test_value_error_handler(test_client_factory): | ||
with create_client( | ||
routes=[Gateway(handler=update)], | ||
exception_handlers={ValueError: value_error_handler}, | ||
) as client: | ||
response = client.put("/update") | ||
assert response.status_code == 400 | ||
|
||
assert ( | ||
response.json()["detail"][0]["msg"] | ||
== "Value error, The name: Esmerald was successfully passed" | ||
) | ||
|
||
|
||
def test_value_error_handler_return_500_on_function(test_client_factory): | ||
with create_client( | ||
routes=[Gateway(handler=raised)], | ||
) as client: | ||
response = client.post("/raise-error") | ||
|
||
assert response.status_code == 500 | ||
|
||
|
||
|