Alow Blueprint routes to explicitly define error_format (#2773)
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
parent
976da69e79
commit
5dd1623192
|
@ -319,6 +319,10 @@ class Blueprint(BaseSanic):
|
||||||
# Prepend the blueprint URI prefix if available
|
# Prepend the blueprint URI prefix if available
|
||||||
uri = self._setup_uri(future.uri, url_prefix)
|
uri = self._setup_uri(future.uri, url_prefix)
|
||||||
|
|
||||||
|
route_error_format = (
|
||||||
|
future.error_format if future.error_format else error_format
|
||||||
|
)
|
||||||
|
|
||||||
version_prefix = self.version_prefix
|
version_prefix = self.version_prefix
|
||||||
for prefix in (
|
for prefix in (
|
||||||
future.version_prefix,
|
future.version_prefix,
|
||||||
|
@ -358,7 +362,7 @@ class Blueprint(BaseSanic):
|
||||||
future.unquote,
|
future.unquote,
|
||||||
future.static,
|
future.static,
|
||||||
version_prefix,
|
version_prefix,
|
||||||
error_format,
|
route_error_format,
|
||||||
future.route_context,
|
future.route_context,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import sanic
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.config import Config
|
from sanic.config import Config
|
||||||
from sanic.errorpages import TextRenderer, exception_response, guess_mime
|
from sanic.errorpages import TextRenderer, exception_response, guess_mime
|
||||||
|
@ -205,6 +206,27 @@ def test_route_error_response_from_explicit_format(app):
|
||||||
assert response.content_type == "text/plain; charset=utf-8"
|
assert response.content_type == "text/plain; charset=utf-8"
|
||||||
|
|
||||||
|
|
||||||
|
def test_blueprint_error_response_from_explicit_format(app):
|
||||||
|
bp = sanic.Blueprint("MyBlueprint")
|
||||||
|
|
||||||
|
@bp.get("/text", error_format="json")
|
||||||
|
def text_response(request):
|
||||||
|
raise Exception("oops")
|
||||||
|
return text("Never gonna see this")
|
||||||
|
|
||||||
|
@bp.get("/json", error_format="text")
|
||||||
|
def json_response(request):
|
||||||
|
raise Exception("oops")
|
||||||
|
return json({"message": "Never gonna see this"})
|
||||||
|
|
||||||
|
app.blueprint(bp)
|
||||||
|
_, response = app.test_client.get("/text")
|
||||||
|
assert response.content_type == "application/json"
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/json")
|
||||||
|
assert response.content_type == "text/plain; charset=utf-8"
|
||||||
|
|
||||||
|
|
||||||
def test_unknown_fallback_format(app):
|
def test_unknown_fallback_format(app):
|
||||||
with pytest.raises(SanicException, match="Unknown format: bad"):
|
with pytest.raises(SanicException, match="Unknown format: bad"):
|
||||||
app.config.FALLBACK_ERROR_FORMAT = "bad"
|
app.config.FALLBACK_ERROR_FORMAT = "bad"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user