Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

error handling in python using flask

from flask import Flask, abort
from auth import AuthError
# depending on the error either 400 or any
#just work with the following

@app.errorhandler(404)
def resource_not_found(error):
  return jsonify({
    "success":  True,
    "error": 404,
    "message": "Resource not found"
  }), 404
##This works for any type of status code error
#You'll follow the same steps just change the error value and message :)

### also for Auth error. 

@app.errorhandler(AuthError)
def AuthError(error):
  """Need to return JSON and we'll have to get a response""" 
  response = jsonify(error)
  response.status_code = error.status_code
  
  return response
Comment

flask error handling

from flask import json
from werkzeug.exceptions import HTTPException

@app.errorhandler(HTTPException)
def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    response = e.get_response()
    # replace the body with JSON
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response
Comment

flask error handling

from werkzeug.exceptions import HTTPException

@app.errorhandler(Exception)
def handle_exception(e):
    # pass through HTTP errors
    if isinstance(e, HTTPException):
        return e

    # now you're handling non-HTTP exceptions only
    return render_template("500_generic.html", e=e), 500
Comment

flask error

from waitress import serve
# app.run(host='0.0.0.0', port=port) # <---- REMOVE THIS
# serve your flask app with waitress, instead of running it directly.
serve(app, host='0.0.0.0', port=port) # <---- ADD THIS
Comment

PREVIOUS NEXT
Code Example
Python :: login_required on class django 
Python :: python unzip a zip 
Python :: how to get images on flask page 
Python :: python update multiple dictionary values 
Python :: day name in python 
Python :: python random list 
Python :: remove env variable python 
Python :: plt .show 
Python :: change django time zone 
Python :: changing plot background color in python 
Python :: python vs c++ 
Python :: Convert DateTime to Unix timestamp in Python 
Python :: how to do disconnect command on member in discord python 
Python :: conda environment 
Python :: script python to download videio from any website 
Python :: python define random variables name 
Python :: apply same shuffle to two arrays numpy 
Python :: how to add an item to a dictionary in python 
Python :: Chi-Squared test in python 
Python :: find the time of our execution in vscode 
Python :: how to make an ai 
Python :: venv 
Python :: print current line number python 
Python :: isntall packages to databricks 
Python :: separate a string in python 
Python :: python ordereddict reverse 
Python :: python float print 2 digits 
Python :: split pandas row into multiple rows 
Python :: how to do swapping in python without 
Python :: get number of zero is a row pandas 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =