mimerender の例外マッピングを使用しています (json を例に取りましょう) が、リクエストが機能している場合とは出力が異なります。
import json
import mimerender
...
mimerender = mimerender.FlaskMimeRender()
render_xml = lambda message: '<message>%s</message>'%message
render_json = lambda **args: json.dumps(args)
render_html = lambda message: '<html><body>%s</body></html>'%message
render_txt = lambda message: message
render_xml_exception = lambda exception: '<exception>%s</exception>'%exception
render_json_exception = lambda exception: json.dumps(exception.args)
render_html_exception = lambda exception: '<html><body>%s</body></html>'%exception
render_txt_exception = lambda exception: exception
@mimerender.map_exceptions(
mapping=(
(ValueError, '500 Internal Server Error'),
(NotFound, '404 Not Found'),
),
default = 'json',
html = render_html_exception,
xml = render_xml_exception,
json = render_json_exception,
txt = render_txt_exception
)
@mimerender(
default = 'json',
html = render_html,
xml = render_xml,
json = render_json,
txt = render_txt
)
def test(...
リクエストが機能すると、次のレスポンスが返されます。
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 29
< Vary: Accept
< Server: Werkzeug/0.8.3 Python/2.7.3rc2
< Date: Tue, 20 Nov 2012 19:27:30 GMT
<
* Closing connection #0
{"message": "Success"}
リクエストが失敗し、例外がトリガーされた場合:
* HTTP 1.0, assume close after body
< HTTP/1.0 401 Not Found
< Content-Type: application/json
< Content-Length: 25
< Vary: Accept
< Server: Werkzeug/0.8.3 Python/2.7.3rc2
< Date: Tue, 20 Nov 2012 19:16:45 GMT
<
* Closing connection #0
["Not found"]
私の質問:例外を除いて、次のように同じ種類の出力が必要です:
{'message': 'Not found'}
これはどのように達成できますか?