ส่งคลาสข้อยกเว้นแบบกำหนดเอง
กำหนดข้อผิดพลาดของโดเมนที่แมปเป็น JSON
ส่งคลาสข้อยกเว้นแบบกำหนดเอง เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Errors That Speak Your Domain
Numbers like 400 are generic. A custom exception names the real problem, such as a payment that failed or a quota hit.
Subclass an Exception
You start by defining a class that inherits from Exception. This gives you a clear, raisable error of your own.
class PaymentError(Exception):
passAdd a Status Code
Give the class a status_code attribute so a handler knows which HTTP code to send when it fires.
class PaymentError(Exception):
status_code = 402Carry a Message
Store a human message in the constructor. Your handler can later read it and place it into the response body.
def __init__(self, message):
self.message = messageRaise It in a View
Inside a view you simply raise the error when something goes wrong. Flask then looks for a matching handler.
if not paid:
raise PaymentError("Card declined")Handle the Custom Class
Register an errorhandler for your class. It receives the instance, so you can read both its message and status code.
@app.errorhandler(PaymentError)
def on_pay(e):
return e.message, e.status_codeReturn JSON From It
For an API, turn the error into JSON inside the handler. Clients get a structured failure they can parse reliably.
return jsonify(error=e.message), e.status_codeOne Base for Many Errors
Make a single base class and let specific errors subclass it. Then one handler can cover your whole error family.
class ApiError(Exception):
passDefault Sensible Values
Give the base a default status_code like 400. Subclasses override it only when they need a different one.
class ApiError(Exception):
status_code = 400Keep Logic Out of Views
Raising domain errors lets views stay short. The handler owns formatting, so each view just signals what went wrong.
Why This Scales
As an app grows, custom exceptions keep error logic in one tidy spot. New error types plug into the same clean pipeline.
Quick Check
How does a view trigger your domain error?
Recap
You built custom exception classes with a status code and message, raised them in views, and mapped them to clean JSON. Awesome!
คำถามที่พบบ่อย
บทเรียน “ส่งคลาสข้อยกเว้นแบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่งคลาสข้อยกเว้นแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่งคลาสข้อยกเว้นแบบกำหนดเอง”
กำหนดข้อผิดพลาดของโดเมนที่แมปเป็น JSON คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่งคลาสข้อยกเว้นแบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- abort และรหัสข้อผิดพลาด HTTP
- ลงทะเบียนฟังก์ชัน errorhandler
- ส่งคลาสข้อยกเว้นแบบกำหนดเอง
- โครงห่อหุ้มข้อผิดพลาด JSON แบบเดียวกัน