sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blueprint ignores error_format option

swheaton opened this issue · comments

commented

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

add_route and its route-decorator friends respect the error_format option to specify "text", "json", or "html", as documented here.
But if I wrap the route in a blueprint, the error_format option no longer works, it just uses the default.

Code snippet

Without Blueprint

from sanic import Sanic
import sanic.exceptions

app = Sanic("MyHelloWorldApp")

@app.get("/", error_format="json")
async def hello_world(request):
    raise sanic.exceptions.SanicException("Big Mistake, Huge.", status_code=400)

// Elsewhere...

$ curl localhost:8000/ -i
HTTP/1.1 400 Bad Request
content-length: 73
connection: keep-alive
content-type: application/json

{"description":"Bad Request","status":400,"message":"Big Mistake, Huge."}

With Blueprint

from sanic import Sanic
import sanic.exceptions

app = Sanic("MyHelloWorldApp")

bp = sanic.Blueprint("MyBlueprint")

@bp.get("/", error_format="json")
async def hello_world(request):
    raise sanic.exceptions.SanicException("Big Mistake, Huge.", status_code=400)

app.blueprint(bp)

// Elsewhere...

$ curl localhost:8000/ -i
HTTP/1.1 400 Bad Request
content-length: 68
connection: keep-alive
content-type: text/plain; charset=utf-8

⚠️ 400 — Bad Request
====================
Big Mistake, Huge.

Expected Behavior

I expect the error_format option to be applied to the blueprint route the same as if applied to an app route.

How do you run Sanic?

Sanic CLI

Operating System

Mac OSX 12.3

Sanic Version

23.3.0

Additional context

No response