تسجيل دوال errorhandler
خصّص الاستجابات لحالتي 404 و500.
تسجيل دوال errorhandler درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «تسجيل دوال errorhandler»؟
خصّص الاستجابات لحالتي 404 و500. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تسجيل دوال errorhandler»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- abort ورموز أخطاء HTTP
- تسجيل دوال errorhandler
- إطلاق فئات استثناء مخصصة
- أغلفة أخطاء JSON موحّدة