Skillversitas

Error Handling

The error is the handler: the 404 (the no the page, the register_error_handler), the 500 (the exception, the log, the no the stack), and the abort (the HTTPException, the 1, the no the return). The errorhandler is the 1 (the status, the template, the no the if), the HTTPException is the class (the code, the name, the no the the), and the log is the error (the logger, the no the print). This chapter covers the abort (the 1, the no the return), the errorhandler (the 404, the 500), the HTTPException (the code, the no the the), and the log (the no the print).

The abort and the errorhandler

# the ABORT (the 1, the no
#   return, the no the 2):
from flask import abort, request

@app.route("/post/<int:id>")
def post_detail(id):
    from .models import Post
    p = db.session.get(Post, id)
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if p is None:
        abort(404)
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    return render_template(
        "post.html", post=p)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 2 (the 400,
#     the 403, the 404,
#     the 405, the 500,
#     the no the 1):
#   abort(403, description=
#       "the no the admin")

# the ERRORHANDLER (the 1,
#   the template, the no
#   the if, the no the 2):
@app.errorhandler(404)
def not_found(e):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if request.accept_mimetypes.best == \
        "application/json":
        return {"error": "the no"}, 404
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    return render_template(
        "404.html"), 404
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

@app.errorhandler(500)
def server_error(e):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    db.session.rollback()
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    app.logger.error(
        "the 500: %s", e)
    return render_template(
        "500.html"), 500
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The abort is the 1: the 404 is the no the page (the no the return, the no the the), the 403 is the no the permission (the description, the no the the), and the 400 is the bad (the 1, the no the the) - the errorhandler is the 1: the @app.errorhandler(404) is the template (the 404.html, the no the if, the no the the), and the JSON is the API (the accept_mimetypes, the no the 1, the no the the) - the 500 is the 2: the db.session.rollback() is the no (the session, the 1, the no the the), and the app.logger.error is the no (the print, the 1, the no the the) - the HTTPException is the next: the code the 1 the no the the.

The HTTPException and the log

# the HTTPEXCEPTION (the
#   code, the 1, the no
#   the the, the no the 2):
from werkzeug.exceptions import \
    HTTPException

@app.errorhandler(HTTPException)
def handle_http(e):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    app.logger.info(
        "%d %s", e.code, e.name)
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    if request.accept_mimetypes.best == \
        "application/json":
        return {"error": e.name}, \
            e.code
    return render_template(
        f"{e.code}.html"), e.code
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 1 (the 400,
#     the 401, the 403,
#     the 404, the 405,
#     the 429, the 500,
#     the no the 1):

# the LOG (the no print, the
#   logger, the no the 1,
#   the no the the):
import logging
app.logger.setLevel(
    logging.INFO)
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the 3 (the info,
#     the warning, the error,
#     the no the 1):
app.logger.info("the start")
app.logger.warning("the no")
app.logger.error("the 500")
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The HTTPException is the 1: the code plus the name is the 2 (the 404 the Not Found, the no the the), and the errorhandler(HTTPException) is the 1 (the all the 4xx the 5xx, the no the 1, the no the the) - the 1 is the no (the 401 the token, the 429 the throttle, the no the the). The log is the no the print: the app.logger is the 1 (the INFO, the no the 1), the 3 level is the 1 (the info, the warning, the error, the no the 1), and the no is the debug (the prod, the 1, the no the the) - the rule is the one: the abort the 1, the errorhandler the template, the HTTPException the 1, and the log the no the print - the next chapter's context is the push the pop the no the 1.

The Working Rules

  • The abort is the 1, the no return. The 404 the no the page (the no the 1, the no the the), and the 403 the description (the no the the) - the 400 the bad (the 1), and the no the 2 (the 401, the 429, the 500, the no the 1).
  • The errorhandler is the 1, the no if. The 404 the template (the 404.html, the no the the), and the JSON the API (the accept_mimetypes, the no the 1) - the 500 the 2 (the rollback, the log, the no the 1), and the no the 1 (the 4xx, the 5xx, the no the the).
  • The HTTPException is the 1, the code the name. The errorhandler(HTTPException) the all (the 4xx, the 5xx, the no the 1), and the 401 the token (the 429, the throttle, the no the the) - the no the 1 (the 405, the method, the no the 1), and the no the the (the 1, the no the 1).
  • The log is the no print, the 3 level. The app.logger the 1 (the INFO, the no the 1), and the 3 (the info, the warning, the error, the no the 1) - the debug the prod (the 1, the no the the), and the no the print (the no the 1, the no the the).

Summary

The error is the handler: the abort the 1 (the 404 the no the page, the 403 the description, the 400 the bad, the no the return), the errorhandler the template (the 404.html, the JSON the API, the no the if), the HTTPException the 1 (the code the name, the 4xx the 5xx the no the 1, the 401 the token, the 429 the throttle), and the log the no the print (the app.logger, the 3, the debug the no the prod). The 500 is the 2 (the rollback the no, the logger the no) - the rule is the one: the abort the 1, the errorhandler the template, the HTTPException the 1, and the log the no the print. The next chapter's context is the app the request: the push, the pop, the no the 1, and the current_app the no the the (the no the 1, the no the the).