Flask – How to create custom abort() code?

The list of possible HTTP status codes is fixed by the Internet Assigned Numbers Authority, so you cannot add a custom one. Werkzeug recognizes this and tries to stop you sending a meaningless code to the browser. Look through the list of status codes to find one that matches your error and use that one.

Edit: Adding status codes to Werkzeug/Flask

import werkzeug.exceptions as ex
from flask import Flask, abort

class PaymentRequired(ex.HTTPException):
    code = 402
    description = '<p>You will pay for this!</p>'

abort.mappings[402] = PaymentRequired

app = Flask(__name__)

@app.route("https://stackoverflow.com/")
def mainpage():
    abort(402)

@app.errorhandler(402)
def payme(e):
    return 'Pay me!'

app.run()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)