errorhandler関数を登録する
404と500のレスポンスをカスタマイズします
「errorhandler関数を登録する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond the Default Page
Flask shows a plain error page out of the box. You can replace it by registering your own errorhandler for any status code.
The errorhandler Decorator
You attach a function to a code with a decorator. This handler runs whenever that error happens anywhere in your app.
@app.errorhandler(404)
def not_found(e):
return "Nothing here", 404Return a Tuple
A handler returns a body and a status code, just like a view. The status code keeps the response honest about what failed.
return "Not found", 404The Error Argument
Each handler receives the error object as its argument. You can read its description or code to shape your reply.
def not_found(e):
return str(e.description), 404Handling 500 Errors
Unexpected crashes become a 500. Register a handler so users see a calm message instead of a raw stack trace.
@app.errorhandler(500)
def boom(e):
return "Server error", 500Render a Template Instead
Most apps return a styled page on error. Just call render_template in the handler and pass the status code along.
return render_template("404.html"), 404Return JSON for APIs
An API should answer errors in JSON, not HTML. Use jsonify so clients can parse the failure cleanly.
return jsonify(error="Not found"), 404Handlers Catch abort Too
Calling abort with a code triggers the matching handler. So one errorhandler covers both crashes and your own aborts.
Handle Exception Classes
You can register a handler by exception type, not just by number. Pass an exception class to catch a whole family at once.
@app.errorhandler(ValueError)
def bad(e):
return "Bad value", 400App-Wide vs Blueprint
Handlers on app apply everywhere, while a blueprint can scope its own. This lets each part of a big app fail its own way.
Why Custom Handlers Help
Consistent error pages keep your brand and your API tidy. A central handler means you fix the message in one place.
Quick Check
Spot the decorator that customizes a missing-page response.
Recap
You can now register errorhandler functions, read the error object, and return HTML or JSON with the right code. Great progress!
よくある質問
「errorhandler関数を登録する」レッスンは無料ですか?
はい。「errorhandler関数を登録する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「errorhandler関数を登録する」で何を学びますか?
404と500のレスポンスをカスタマイズします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「errorhandler関数を登録する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- abortとHTTPエラーコード
- errorhandler関数を登録する
- カスタム例外クラスを送出する
- 統一されたJSONエラーエンベロープ